자료구조 - Singly Linked List 구현
7453 단어 data structuresJava자료구조Java
싱글 링크드 리스트 구현해보는 포스팅입니다.
Singly Linked List는 각 Node를 단방향으로 연결합니다.
각 Node는 데이터와 다음 연결될 Node를 가진다.
맨 처음 Node는 head 입니다.
Node.next 가 null 이면 LinkedList의 tail 마지막 값입니다.
코드
자바입니다.
public class SinglyLinkedList{
private ListNode head; //
//It contains a static inner class ListNode
private static class ListNode{
private int data; //Generic Type
private ListNode next;
public ListNode(int data){
this.data = data;
this.next = null;
}
}
public static void main(String[] args){
//Lets create a linked list demonstrated in slide
//10 --> 8 --> 1 --> 11 --> null
//10은 LinkedList Node의 head 값입니다.
//ListNode head = new ListNode(10);
SinglyLinkedList sil = new SinglyLinkedList();
sill.head = new ListNode(10);
ListNode second = new ListNode(8);
ListNode third = new ListNode(1);
ListNode fourth = new ListNode(11);
//각 노드들을 연결합니다.
sil.head.next = second; // 10 --> 8 --> null
second.next = third; // 10 --> 8 --> 1 -->null
third.next = fourth; //10 --> 8 --> 1 --> 11 --> null
sil.display();//10 --> 8 --> 1 --> 11 --> null
}
//ListNode display
public void display(){
ListNode current = head;
while(current != null){
System.out.print(current.data + " --> ");
current = current.next;
}
System.out.print("null");
}
}
참고
https://www.youtube.com/watch?v=-j6LVWmyCCU
Author And Source
이 문제에 관하여(자료구조 - Singly Linked List 구현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yamamamo/자바-Singly-Linked-List저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)