포스트

LinkedNodeList 구현

LinkedNodeList 구현

이전시간에 Singly LinkedList를 구현해보았는데요.

SinglyLinkedList에서는 첫번째 노드가 Header 노드이면서 첫번째 노드로 코드를 작성했어요.

그렇게 되면 첫번째 노드를 삭제하려고 할때 에러가 발생할거에요. 왜냐하면 LinkedList의 시작이 첫번째 노드인데 이 노드를 지워버리면 2번째 노드를 불러올수 없거든요

다음과 같이 헤더 노드를 추가하고 그 클래스 안에 노드 클래스와 변수를 선언 한뒤에, 헤더 노드로 시작한 리스트를 구현해볼거에요

여기서 헤더노드는 단순히 시작을 가르키는 노드라고 생각하면 쉬울거에요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**  
 * 링크드 노드 리스트 구현  
 */  
  
class LinkedNodeList {
	// 헤더 노드
    Node header;  

	// 링크드 노드 리스트 생성자
    LinkedNodeList() {  
        header = new Node();  
    }  

	// 노드 클래스
	static class Node {  
        int data;  
  
        Node next = null;  
		
        Node () {  
        }  
        
        Node (int d) {  
            data = d;  
            next = null;  
        }  
    }  

	// 노드 삽입
    void append (int d) {  
	    // 추가될 노드 생성
        Node newNode = new Node(d);  

		// 시작 노드 할당
        Node nowNode = header;  

		// 제일 마지막 노드까지 반복문
        while (nowNode.next != null) {  
            nowNode = nowNode.next;  
        }  

		// 마지막 노드에 새로운 노드 연결
        nowNode.next = newNode;  
    }  

	// 노드 삭제
    void delete (int d) {
        // 시작 노드 할당
        Node nowNode = header;  

		// 마지막 노드까지 반복문
        while (nowNode.next != null) {  
		    // 다음 노드의 data와 d가 일치하는지 확인
            if (nowNode.next.data == d) {  
	            // 일치한다면 그 다음 노드의 포인터를 저장
                nowNode.next = nowNode.next.next;  
            }else{  
	            // 일치하지 않는다면 다음 노드로 이동
                nowNode = nowNode.next;  
            }  
        }  
    }  
  
    void retrieve () {  
	    // 헤더는 시작을 알리는 노드로 다음 노드부터 반복문 실행
        Node nowNode = header.next;  
        while (nowNode.next != null ) {  
            System.out.print(nowNode.data + " -> ");  
            nowNode = nowNode.next;  
        }  
  
        System.out.println(nowNode.data);  
    }  
}  
  
  
  
public class test {  
    public static void main(String[] args) {  
        LinkedNodeList linkedNodeList = new LinkedNodeList();  
        linkedNodeList.append(1);  
        linkedNodeList.append(2);  
        linkedNodeList.append(3);  
        linkedNodeList.append(4);  
  
        linkedNodeList.retrieve();  
        // 1 -> 2 -> 3 -> 4
  
        linkedNodeList.delete(1);  
  
        linkedNodeList.retrieve();  
        // 2 -> 3 -> 4
    }  
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.