-
Notifications
You must be signed in to change notification settings - Fork 4
/
Queue.py
67 lines (55 loc) · 1.32 KB
/
Queue.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
class Node:
def __init__(self, data, next = None) :
self.data = data
self.next = next
class Queue :
def __init__(self) :
self.__head = None
self.__tail = None
self.__size = 0
def size(self) :
return self.__size
def isEmpty(self) :
return self.size() == 0
def push(self, data) :
newNode = Node(data)
if(self.isEmpty()) :
self.__head = self.__tail = newNode
else :
self.__tail.next = newNode
self.__tail = newNode
self.__size += 1
def front(self) :
if(self.isEmpty()) :
raise Exception("Queue is Empty")
else :
return self.__head.data
def pop(self) :
if(self.isEmpty()):
raise Exception("Queue is Empty")
else:
temp = self.__head
self.__head = self.__head.next
ret = temp.data
del temp
self.__size -= 1
return ret
def __str__(self):
l = []
trav = self.__head
while trav is not None :
l.append(str(trav.data))
trav = trav.next
return '->'.join(l)
#Test
q = Queue()
q.push(5)
q.push(10)
q.push(15)
print(q)
q.push(13)
print(q)
print(q.pop())
print(q)
print(q.front())
print(q)