-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueLL.py
66 lines (56 loc) · 1.7 KB
/
QueueLL.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __str__(self):
return self.data
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def __iter__(self):
current_node = self.head
while current_node:
yield current_node
current_node = current_node.next
class Queue:
def __init__(self):
self.linked_list = LinkedList()
def __str__(self):
data = [str(x) for x in self.linked_list]
return ' '.join(data)
def enqueue(self, data):
node = Node(data)
if self.linked_list.head is None:
self.linked_list.head = node
self.linked_list.tail = node
else:
self.linked_list.tail.next = node
self.linked_list.tail = node
def isEmpty(self):
if self.linked_list.head is None:
return True
else:
return False
def dequeue(self):
if self.isEmpty():
print("Queue is empty!! Aborting...")
return
else:
node = self.linked_list.head
if self.linked_list.head == self.linked_list.tail:
self.linked_list.head = None
self.linked_list.tail = None
else:
self.linked_list.head = self.linked_list.head.next
return node
def peek(self):
if self.isEmpty():
print("Queue is empty!! Aborting...")
return
else:
node = self.linked_list.head
print(node)
def delete(self):
self.linked_list.head = None
self.linked_list.tail = None