-
Notifications
You must be signed in to change notification settings - Fork 401
/
Queue_using_LL.py
39 lines (34 loc) · 989 Bytes
/
Queue_using_LL.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
class Node:
def __init__(self, data = None):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.head = None
self.last = None
def enqueue(self, data):
if not self.last:
self.head = Node(data)
self.last = self.head
else:
self.last.next = Node(data)
self.last = self.last.next
def dequeue(self):
if not self.head:
return None
val = self.head.data
self.head = self.head.next
return val
def display(self):
temp = self.head
while temp != None:
print(temp.data)
temp = temp.next
x = Queue() # Creating object of queue class
x.enqueue(1) # Add 1 to the queue
x.enqueue(2)# Add 2 to the queue
x.display() # 1 => 2
print(x.dequeue()) # Deleting the first element of the queue.
x.display() # 2
print(x.dequeue()) # 2
print(x.dequeue()) # None(because queue is already empty)