-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedQueuestack.py
152 lines (116 loc) · 3.15 KB
/
LinkedQueuestack.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class linkedqueue :
'''Linked list implementation of queue
'''
def __init__(self):
self._qhead = None
self._qtail = None
self._count = 0
def is_empty(self):
return self._count == 0
def enqueue(self, item):
e_node = _qnode(item)
if self.is_empty():
self._qhead = e_node
else:
self._qtail.next = e_node # line no 20 e_node.next
self._qtail = e_node
self._count += 1
def dequeue(self):
if not self.is_empty():
item = self._qhead.data
self._qhead = self._qhead.next
self._count -= 1
return item
def __len__(self):
return self._count
class _qnode(object):
def __init__(self, data):
self.data = data
self.next = None
class circularLinkedList:
def __init__(self):
self.tail = None
self.size = 0
def __len__(self):
return self.size
def is_empty(self):
return self.size == 0
def enqueue(self, item):
node = _qnode(item)
if self.is_empty():
node.next = node
else:
node.next = self.tail.next
self.tail.next = node
self.tail = node
self.size += 1
def traversal(self):
current = self.tail
count = 0
while count < len(self) :
print(current.data)
current = current.next
count += 1
def dequeue(self):
if not self.is_empty():
node = self.tail.next
if self.size == 1:
self.tail = None
else:
self.tail.next = node.next
self.size -= 1
return node.data
def rotate(self):
if self.size > 0:
self.tail = self.tail.next
def traversal(self):
current = self.tail
count = 0
while count < len(self) :
print(current.data)
current = current.next
count += 1
'''circularll = circularLinkedList()
circularll.enqueue(12)
circularll.enqueue(-1)
circularll.enqueue(22)
circularll.enqueue(2)
circularll.traversal()'''
class orderedCircularLinkedList:
def __init__(self):
self.tail = None
self.size = 0
def __len__(self):
return self.size
def is_empty(self):
return self.size == 0
def enqueue(self, item):
current = self.tail
previous = None
count = 0
stop = False
while count < len(self) and not stop :
if current.data > item :
stop = True
else:
previous = current
current = current.next
node = _qnode(item)
if previous is None:
node.next = node
else:
previous.next = node
self.tail.next = node
self.tail = node
self.size += 1
'''oo = orderedCircularLinkedList()
oo.enqueue(12)
oo.enqueue(-1)
oo.enqueue(22)
oo.enqueue(2)
oo.traversal()
llqueue = linkedqueue()
for i in range(4):
llqueue.enqueue(i)
for i in range(4):
print(llqueue.dequeue())'''