Skip to content

Commit 52cc8f4

Browse files
Added Queue and QueueLL code.
1 parent 2dd289b commit 52cc8f4

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

‎Queues/queues.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import os
2+
3+
4+
class Queue:
5+
def __init__(self):
6+
self._data = []
7+
8+
def __len__(self):
9+
'''
10+
Returns length of Queue
11+
'''
12+
return len(self._data)
13+
14+
def isempty(self):
15+
'''
16+
Returns True if Queue is empty, otherwise False.
17+
'''
18+
return self.__len__ == 0
19+
20+
def enqueue(self, e):
21+
'''
22+
Adds the passed element at the end of the list.
23+
That means, it enqueues element.
24+
'''
25+
self._data.append(e)
26+
27+
def dequeue(self):
28+
'''
29+
Removes element from the beginning of the list and
30+
returns the removed element.
31+
That means, it dequeues.
32+
'''
33+
if self.isempty():
34+
print("Queue is empty.")
35+
return
36+
37+
return self._data.pop(0)
38+
39+
def first(self):
40+
'''
41+
Peeks and return the first element in the Queue.
42+
'''
43+
if self.isempty():
44+
print("Queue is empty.")
45+
return
46+
47+
return self._data[0]
48+
49+
def display(self):
50+
'''
51+
Utility function to display the Queue.
52+
'''
53+
if self.isempty():
54+
print("Queue is empty.")
55+
else:
56+
print("Front", end=' <--')
57+
for i in self._data:
58+
print(i, end='<--')
59+
print(" Rear")
60+
61+
###############################################################################
62+
63+
64+
def options():
65+
'''
66+
Prints Menu for operations
67+
'''
68+
options_list = ['Enqueue', 'Dequeue', 'First',
69+
'Display Queue', 'Exit']
70+
71+
print("MENU")
72+
for i, option in enumerate(options_list):
73+
print(f'{i + 1}. {option}')
74+
75+
choice = int(input("Enter choice: "))
76+
return choice
77+
78+
79+
def switch_case(choice):
80+
'''
81+
Switch Case for operations
82+
'''
83+
os.system('cls')
84+
if choice == 1:
85+
elem = int(input("Enter item to Enqueue: "))
86+
Q.enqueue(elem)
87+
88+
elif choice == 2:
89+
print('Dequeued item is: ', Q.dequeue())
90+
91+
elif choice == 3:
92+
print("First item is: ", Q.first())
93+
94+
elif choice == 4:
95+
print("Queue: ")
96+
Q.display()
97+
print("\n")
98+
99+
elif choice == 5:
100+
import sys
101+
sys.exit()
102+
103+
###############################################################################
104+
105+
106+
Q = Queue()
107+
while True:
108+
choice = options()
109+
switch_case(choice)

‎Queues/queuesLL.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import os
2+
3+
4+
class _Node:
5+
'''
6+
Creates a Node with two fields:
7+
1. element (accesed using ._element)
8+
2. link (accesed using ._link)
9+
'''
10+
__slots__ = '_element', '_link'
11+
12+
def __init__(self, element, link):
13+
'''
14+
Initialses _element and _link with element and link respectively.
15+
'''
16+
self._element = element
17+
self._link = link
18+
19+
20+
class QueueLL:
21+
'''
22+
Consists of member funtions to perform different
23+
operations on the linked list.
24+
'''
25+
26+
def __init__(self):
27+
'''
28+
Initialses head, tail and size with None, None and 0 respectively.
29+
'''
30+
self._head = None
31+
self._tail = None
32+
self._size = 0
33+
34+
def __len__(self):
35+
'''
36+
Returns length of Queue
37+
'''
38+
return self._size
39+
40+
def isempty(self):
41+
'''
42+
Returns True if Queue is empty, otherwise False.
43+
'''
44+
return self._size == 0
45+
46+
def enqueue(self, e):
47+
'''
48+
Adds the passed element at the end of the linked list.
49+
That means, it enqueues element.
50+
'''
51+
newest = _Node(e, None)
52+
53+
if self.isempty():
54+
self._head = newest
55+
else:
56+
self._tail._link = newest
57+
58+
self._tail = newest
59+
self._size += 1
60+
61+
def dequeue(self):
62+
'''
63+
Removes element from the beginning of the linked list and
64+
returns the removed element.
65+
That means, it dequeues.
66+
'''
67+
if self.isempty():
68+
print("Queue is Empty. Cannot perform dequeue operation.")
69+
return
70+
71+
e = self._head._element
72+
self._head = self._head._link
73+
self._size = self._size - 1
74+
75+
if self.isempty():
76+
self._tail = None
77+
return e
78+
79+
def first(self):
80+
'''
81+
Peeks and return the first element in the Queue.
82+
'''
83+
if self.isempty():
84+
print("Queue is Empty.")
85+
return
86+
e = self._head._element
87+
return e
88+
89+
def display(self):
90+
'''
91+
Utility function to display the Queue.
92+
'''
93+
if self.isempty() == 0:
94+
p = self._head
95+
print("Front", end=' <--')
96+
while p:
97+
print(p._element, end='<--')
98+
p = p._link
99+
print(" Rear")
100+
else:
101+
print("Empty")
102+
103+
###############################################################################
104+
105+
106+
def options():
107+
'''
108+
Prints Menu for operations
109+
'''
110+
options_list = ['Enqueue', 'Dequeue', 'First',
111+
'Display Queue', 'Exit']
112+
113+
print("MENU")
114+
for i, option in enumerate(options_list):
115+
print(f'{i + 1}. {option}')
116+
117+
choice = int(input("Enter choice: "))
118+
return choice
119+
120+
121+
def switch_case(choice):
122+
'''
123+
Switch Case for operations
124+
'''
125+
os.system('cls')
126+
if choice == 1:
127+
elem = int(input("Enter item to Enqueue: "))
128+
Q.enqueue(elem)
129+
130+
elif choice == 2:
131+
print('Dequeued item is: ', Q.dequeue())
132+
133+
elif choice == 3:
134+
print("First item is: ", Q.first())
135+
136+
elif choice == 4:
137+
print("Queue: ")
138+
Q.display()
139+
print("\n")
140+
141+
elif choice == 5:
142+
import sys
143+
sys.exit()
144+
145+
###############################################################################
146+
147+
148+
Q = QueueLL()
149+
while True:
150+
choice = options()
151+
switch_case(choice)

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ Find the detailed description on operations performed [here.](Linked%20List/)
2727

2828
1. [Stack using inbuilt Python List](Stacks/stack.py)
2929
2. [Stack using Linked List](Stacks/stackLL.py)
30+
31+
### 3. Queue
32+
33+
1. [Queue using inbuilt Python List](Queues/queues.py)
34+
2. [Queue using Linked List](Queues/queuesLL.py)

0 commit comments

Comments
 (0)