-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
87 lines (71 loc) · 2.02 KB
/
queue.py
File metadata and controls
87 lines (71 loc) · 2.02 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
# LAB11.py
class Node:
def __init__(self, value):
self.value = value
self.next = None
def getValue(self):
return self.value
def getNext(self):
return self.next
def setValue(self, new_value):
self.value = new_value
def setNext(self, new_next):
self.next = new_next
def __str__(self):
return "{}".format(self.value)
__repr__ = __str__
class Queue:
def __init__(self):
self.count = 0
self.head = None
self.tail = None
def isEmpty(self):
if self.count == 0:
return True
else:
return False
def size(self):
return self.count
def enqueue(self, item):
newNode = Node(item)
if self.count == 0:
self.head = newNode
self.tail = self.head
elif self.count == 1:
self.tail = newNode
self.head.next = self.tail
else:
current = self.head
for i in range(self.count - 1):
current = current.next
current.next = newNode
self.tail = newNode
self.count += 1
def dequeue(self):
if self.count == 0:
return "Queue is empty"
elif self.count == 1:
self.count -= 1
removed = self.head.value
self.head = None
return removed
elif self.count == 2:
self.count -= 1
removed = self.head.value
self.head, self.tail = self.tail, None
return removed
else:
self.count -= 1
removed = self.head.value
newHead = self.head.next
del self.head
self.head = newHead
return removed
def printQueue(self):
temp = self.head
while temp:
print(temp.value, end=' ')
temp = temp.next
print()
# Collaboration Statement:
# I worked on the homework(lab) assignment alone, using only previous and current course materials.