-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circular_linked_list.py
128 lines (111 loc) · 3.73 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
Circular linked list is circular type linked list.
which in this case track form tail.
One of the great thing that is better than simple linked list is it can access all element by roltating.
ALso it is easy to put element at front or tail or detlete front or tail(Reason :Track from tail).
'''
class CList:
class Node:
def __init__(self, item,nexts): # Make Node : item for storage, next linking next node
self.item = item
self.next = nexts
def __init__(self): #Starting CList initializing
self.last = None
self.size = 0
def lens(self): #lens : number of items in linked list
return self.size
def is_empty(self): # Check if Circular list is empty
return self.size == 0
def insert_first(self, item): # Inserting new node to first circular list
n = self.Node(item, None)
if self.is_empty():
n.next = n
self.last=n
else:
n.next = self.last.next
self.last.next = n
self.size += 1
def insert_last(self, item): # Inserting new node to first circular list
n = self.Node(item, None)
if self.is_empty():
n.next = n
self.last=n
else:
n.next = self.last.next
self.last.next = n
self.last = n
self.size += 1
def first(self): # Show first item
if self.is_empty():
raise EmptyError("List is empty")
f = self.last.next
return f.item
def print_list(self): # Print and show the linked list
if self.is_empty():
print("list is empty")
else:
f = self.last.next
p = f
while p.next != f:
print(p.item,"-> ", end="")
p = p.next
print(p.item)
def delete_first(self): # Delete first node
if self.is_empty():
raise EmptyError("List is empty")
x = self.last.next
if self.size == 1:
self.last = None
else:
self.last.next = x.next
self.size -=1
def delete_last(self): # Delete last node
if self.is_empty():
raise EmptyError("List is empty")
x = self.last
if self.size == 1:
self.last = None
else:
while(x.next != self.last):
x = x.next
self.last = x
self.last.next=x.next.next
self.size -=1
def search_by_name(self,name): # Search item by it`s name
if self.is_empty():
raise EmptyError("List is empty")
f = self.last.next
x = f
count = 0
while (x.item != name):
if (x.next == f):
return "There is no item with such name"
count = count + 1
x = x.next
return count
class EmptyError(Exception):
pass
if __name__=="__main__": # Test this data structure
c = CList()
c.insert_first("b")
c.insert_last("c")
c.insert_first("a")
c.insert_last("d")
c.print_list()
print("Find index of node item(c) :", c.search_by_name("c"))
print("Length of S is :",c.lens())
print("First node of S",c.first())
c.delete_last()
print("Afeter deleting last node :",end="")
c.print_list()
print("Length of S is :",c.lens())
print("First node of S",c.first())
c.delete_last()
print("Afeter deleting last node :",end="")
c.print_list()
c.delete_first()
print("Afeter deleting first node :",end="")
c.print_list()
c.delete_first()
print("Afeter deleting first node :",end="")
c.print_list()