7JeY world

[자료구조 / Python] 단일 연결 리스트 | Singly Linked List using Python 본문

Programming/Data Structure

[자료구조 / Python] 단일 연결 리스트 | Singly Linked List using Python

7JeY 2021. 4. 11. 14:33
반응형
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
95
96
97
98
99
100
101
102
103
104
class SList:
 
    class Node:
        def __init__(self, item, link): #노드 생성자
            self.item = item
            self.next = link
 
    
    def __init__(self): #연결 리스트 생성자
        self.head = None
        self.size = 0
 
    def size(self):
        return self.size
 
    def isEmpty(self):
        return self.size == 0
 
    def insertFront(self, item): #연결리스트 맨 앞에 새 노드 삽입
        if self.isEmpty():
            self.head = self.Node(item, None)
        else:
            self.head = self.Node(item, self.head) #head가 새 노드 참조 ??
        self.size += 1
 
    def insertAfter(self, item, num): #num이 가리키는 노드 다음에 새 노드 삽입
        num.next = SList.Node(item, num.next#왜 SList.Node    ~ 일까?
        self.size += 1
 
    def deleteFront(self):
        if self.isEmpty():
            return "Underflow"
        else:
            self.head = self.head.next #둘째 노드 참조하도록
            self.size -= 1
 
    def deleteAfter(self, num):
        if self.isEmpty():
            return "Underflow"
        temp = num.next
        num.next = temp.next
        del temp # 해도 되나 ..? 내가 추가함
        self.size -= 1
 
    def select(self, target): #노드 탐색(순차 탐색)    
        p = self.head
        for k in range(self.size):
            if target == p.item:
                return k #탐색 성공
            p = p.next
        return None #탐색 실패
 
    def print_list(self):
        p = self.head
        while p:
            if p.next != None:
                print(p.item, '->', end= '')
            else:
                print(p.item)
            p = p.next #노드순차탐색
 
 
class EmptyError(Exception):
    pass
 
 
= SList()
s.insertFront('orarnge')
s.insertFront('apple')
s.insertAfter('cherry', s.head.next)
s.print_list()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cs
반응형
Comments