-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.py
40 lines (31 loc) · 840 Bytes
/
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
#!/usr/bin/env python3
from nodes import SLLNode
class Queue:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def enqueue(self, data):
node = SLLNode(data)
if self.head is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
self.size += 1
def dequeue(self):
if self.head is not None:
data = self.head.data
self.head = self.head.next
self.size -= 1
return data
else:
raise Exception('Queue is empty')
def peek(self):
if self.head is not None:
return self.head.data
else:
raise Exception('Queue is empty')
def isempty(self):
return self.size == 0
#