-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.py
More file actions
288 lines (237 loc) · 6.97 KB
/
lists.py
File metadata and controls
288 lines (237 loc) · 6.97 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Simple Data Structures
Includes Linked List, Queue, and Stack
Three implementations of Queue: Queue, Improved Queue, and Priority Queue
"""
import numpy as np
from pprint import pprint as pp
from random import randrange
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __str__(self):
return "node data = " + str(self.data)
def print_list(self):
current = self
while current != None:
if current.next == None:
print (str(current.data),end="")
else:
print (str(current.data),end=", ")
current = current.next
def print_backward(self):
if self.next != None:
tail = self.next
tail.print_backward()
print (self.data),
class LinkedList:
def __init__(self):
self.length = 0
self.head = None
def __str__(self):
return "head data = " + str(self.head.data)
def is_empty(self):
return (self.head is None)
def insert(self, data):
node = Node(data)
node.next = self.head
self.head = node
self.length = self.length + 1
def make_circular(self,data):
node = Node(data)
node.next = None
if self.head == None:
# if list is empty the new node goes first
self.head = node
else:
# find the last node in the list
last = self.head
while last.next: last = last.next
# append the new node
last.next = node
# point the new node to the head
node.next = self.head
self.length = self.length + 1
def delete(self, data):
prev = None
current = self.head
while current.data is not None:
if current.data is data:
break
prev = current
current = current.next
# deleted node is not present
if current.data is None:
return self.head
# deleted node is first node
if current is self.head:
return current.next
# otherwise
prev.next = current.next
return self.head
def delete_first(self):
data = self.head.data
self.head = self.head.next
self.length = self.length - 1
return data
"""Return True if linked list is circular, else False.
This works by keeping two pointers, faster and slower.
Faster is incremented twice each iteration and slower
is incremented once. If there is a collision, then we
know it is a circular list, if faster is None, we know
the list is not circular."""
def is_circular(self):
slower = self.head
faster = self.head.next
while True:
if faster is None or faster.next is None:
return False # list isn't circular
elif faster is slower or faster.next is slower:
return True
else:
slower = slower.next #advance once
faster = faster.next.next #advance twice
def print_list(self):
if not self.is_circular():
print ("[",end="")
if self.head != None:
self.head.print_list()
print ("]")
else:
print ("Circular List")
def print_backward(self):
print ("["),
if self.head != None:
self.head.print_backward()
print ("]")
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return (self.items == [])
class Queue:
def __init__(self):
self.length = 0
self.head = None
def is_empty(self):
return (self.length == 0)
def insert(self, data):
node = Node(data)
node.next = None
if self.head == None:
# if list is empty the new node goes first
self.head = node
else:
# find the last node in the list
last = self.head
while last.next: last = last.next
# append the new node
last.next = node
self.length = self.length + 1
def remove(self):
data = self.head.data
self.head = self.head.next
self.length = self.length - 1
return data
def print_queue(self):
print ("[",end="")
while not self.is_empty():
if self.length==1:
print (self.remove(),end="")
else:
print (str(self.remove()),end=", ")
print ("]")
class ImprovedQueue(Queue):
def __init__(self):
self.length = 0
self.head = None
self.tail = None
def is_empty(self):
return (self.length == 0)
def insert(self, data):
node = Node(data)
node.next = None
if self.length == 0:
# if list is empty, the new node is head and last
self.head = self.tail = node
else:
# find the last node
last = self.tail
# append the new node
last.next = node
self.tail = node
self.length = self.length + 1
def remove(self):
data = self.head.data
self.head = self.head.next
self.length = self.length - 1
if self.length == 0:
self.tail = None
return data
# Inefficient (using linked list instead of binary heap)
class PriorityQueue(Queue):
def __init__(self):
self.items = []
self.length = 0
def is_empty(self):
return self.items == []
def insert(self, item):
self.items.append(item)
self.length += 1
def remove(self):
maxi = 0
for i in range(1, len(self.items)):
if self.items[i] > self.items[maxi]: maxi = i
item = self.items[maxi]
self.items[maxi:maxi+1] = []
self.length -= 1
return item
my_list = [1,7,4,2,9,3,6,8,0,5]
random_list = [randrange(100) for i in range(20)]
# Linked List
title = "Linked List"
print (title)
print ("=" * len(title))
ll = LinkedList()
for i in range(len(my_list)):
ll.insert(my_list[i])
ll.print_list(); print()
# Queue
title = "Queue"
print (title)
print ("=" * len(title))
q = Queue()
for i in range(len(my_list)):
q.insert(my_list[i])
q.print_queue(); print()
# IQueue
title = "Improved Queue"
print (title)
print ("=" * len(title))
iq = Queue()
for i in range(len(my_list)):
iq.insert(my_list[i])
iq.print_queue(); print()
# PQueue
title = "Priority Queue"
print (title)
print ("=" * len(title))
pq = PriorityQueue()
for i in range(len(my_list)):
pq.insert(my_list[i])
pq.print_queue(); print()
# Test for a circular linked list
title = "Print a circular linked list"
print (title)
print ("=" * len(title))
# create a circular list
ll = LinkedList()
for i in range(len(my_list)):
ll.insert(my_list[i])
ll.make_circular(4)
ll.print_list()