-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-circular-linked-list.py
61 lines (53 loc) · 1.69 KB
/
python-circular-linked-list.py
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
#!/usr/bin/python3
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def prepend(self, data):
new_node = Node(data)
current = self.head
new_node.next = self.head
# first case, no other nodes
if not self.head:
new_node.next = new_node
else:
# second case, there is a node in the list
while current.next != self.head:
current = current.next
current.next = new_node
self.head = new_node
def append(self, data):
# first case, there is no head
# set head to data passed in
# set pointer to next node as head (itself)
if not self.head:
self.head = Node(data)
self.head.next = self.head
else:
# second case, there is a node already
# set new node equal to the data passed in
# traverse the list until we hit the node that points to the head
# have that node point to new_node
# set new_node to point to head
new_node = Node(data)
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def print_list(self):
current = self.head
while current:
print(current.data)
current = current.next
if current == self.head:
break
cllist = CircularLinkedList()
cllist.append("C")
cllist.append("D")
cllist.prepend("B")
cllist.prepend("A")
cllist.print_list()