-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularlinkedlist.py
More file actions
80 lines (70 loc) · 2.02 KB
/
Copy pathcircularlinkedlist.py
File metadata and controls
80 lines (70 loc) · 2.02 KB
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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def insert_first(self, data):
new_node = Node(data)
if not self.head:
new_node.next = new_node
self.head = new_node
else:
tail = self.head
while tail.next != self.head:
tail = tail.next
new_node.next = self.head
tail.next = new_node
self.head = new_node
def insert(self, prev_node, data):
if not prev_node:
print("이전 노드가 리스트에 존재하지 않습니다.")
return
new_node = Node(data)
new_node.next = prev_node.next
prev_node.next = new_node
def delete_first(self):
if not self.head:
return
if self.head.next == self.head:
self.head = None
else:
tail = self.head
while tail.next != self.head:
tail = tail.next
self.head = self.head.next
tail.next = self.head
def delete(self, prev_node):
if not prev_node or not prev_node.next:
print("삭제할 노드가 존재하지 않습니다.")
return
prev_node.next = prev_node.next.next
def print_list(self):
if not self.head:
print("NULL")
return
temp = self.head
while True:
print(f"{temp.data}->", end="")
temp = temp.next
if temp == self.head:
break
print("(head)")
llist = CircularLinkedList()
print("[첫 위치 삽입]")
for i in range(5):
llist.insert_first(i)
llist.print_list()
if i == 2:
temp = llist.head
print("[중간 위치 삽입]")
llist.insert(temp, 100)
llist.print_list()
print("[중간 위치 삭제]")
llist.delete(temp)
llist.print_list()
print("[첫 위치 삭제]")
for i in range(5):
llist.delete_first()
llist.print_list()