-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.1-Circular_Linked_List.py
103 lines (79 loc) · 2.29 KB
/
4.1-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
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
"""_summary_
Circular Linked List Implementation.
"""
__author__='Metehan Özcan'
class Node:
#* Constructor to create a new node
def __init__(self, data,next=None):
self.data = data
self.next = next
class CircularLinkedList:
#* Constructor to create a empty circular linked list
def __init__(self):
self.head = None
self.size =0
def prepend(self,data): #* Add new node at beginning
newNode=Node(data,self.head)
curr=self.head
if not self.head: #* If List is empty.
newNode.next=newNode
else:
while curr.next!=self.head:
curr=curr.next
curr.next=newNode
self.head=newNode
self.size+=1
return
def append(self,data): #* Add new node to the end
if self.size ==0:
self.prepend(data)
newNode=Node(data)
curr=self.head
while curr.next!=self.head:
curr=curr.next
curr.next=newNode
newNode.next=self.head
self.size+=1
return
def delete(self): #* Deletes Head
curr=self.head
while curr.next !=self.head:
curr=curr.next
curr.next=self.head.next
self.head=self.head.next
self.size-=1
return
def pop(self): #* Deletes Last Node
curr=self.head
while curr.next.next != self.head:
curr=curr.next
curr.next=self.head
self.size-=1
return
#* Function to print nodes in a given circular linked list
def printList(self):
curr = self.head
last=self.head
s=''
if self.head is not None:
while(True):
s+=str(curr.data)+'--->'
curr = curr.next
if (curr == self.head):
break
s+='To The Head Again->'+str(last.data)
print(s)
return
#*Created Linked List
cll = CircularLinkedList()
#* Created linked list will be 2--->1--->4--->5--->To The Head Again->2
cll.prepend(1)
cll.prepend(2)
cll.prepend(3)
cll.append(4)
cll.append(5)
cll.append(6)
cll.delete()
cll.pop()
print ("Circular Linked List;")
cll.printList()