Skip to content

Commit 5384f44

Browse files
authored
Create Queue.py
1 parent 1b8e041 commit 5384f44

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Data_Structure/src/Queue.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Queue:
2+
3+
def __init__(self, Size = 10):
4+
self.__size = Size
5+
self.__queue = [None]*Size
6+
self.__head = self.__tail = 0
7+
self.__i = -1
8+
9+
def isEmpty(self):
10+
return self.__head == self.__tail
11+
12+
def isFull(self):
13+
return self.__tail == self.__size
14+
15+
def EnQueue(self, value):
16+
if(self.isFull()): raise IndexError("Overflow Error - Queue is Full")
17+
self.__queue[self.__tail] = value
18+
self.__tail += 1
19+
20+
def DeQueue(self):
21+
if(self.isEmpty()): raise IndexError("Unerflow Error - Queue is Empty")
22+
self.__head += 1
23+
return self.__queue[self.__head-1]
24+
25+
def __len__(self):
26+
return self.__tail - self.__head
27+
28+
def __getitem__(self, index):
29+
if(self.__head <= (index + self.__head) < self.__tail):
30+
return self.__queue[index + self.__head]
31+
else: raise IndexError("Index out of range")
32+
33+
def __next__(self):
34+
self.__i += 1
35+
if(self.__i < len(self)): return self.__getitem__(self.__i)
36+
else: raise StopIteration()
37+
38+
def __iter__(self):
39+
self.__i = -1
40+
return self
41+
42+
43+
if __name__ == '__main__':
44+
45+
print("\nCreated Queue of size 5")
46+
Q1 = Queue(5)
47+
48+
print("\nEnQueue (Insert) 5")
49+
Q1.EnQueue(5)
50+
print("EnQueue (Insert) 15")
51+
Q1.EnQueue(15)
52+
53+
print("\nPrint Queue")
54+
for i in Q1:
55+
print(i, end=", ")
56+
57+
print("\n\nDeQueue (Delete), Deleted element = ", Q1.DeQueue())
58+
print("DeQueue (Delete), Deleted element = ", Q1.DeQueue())
59+
60+
print("\nQueue empty\n")
61+
62+
print("\n\nDeQueue / Delete, Deleted element = ", Q1.DeQueue())

0 commit comments

Comments
 (0)