Skip to content

Commit bd77fc1

Browse files
committed
Queue
1 parent 532a9b9 commit bd77fc1

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ You can implement these notes in your own favourite programming language.
3838
- [x] [Binary Search Tree](Trees/binarysearchtree.py)
3939
- [x] [Stack And Queue](Trees)
4040
- [x] [Stack](Stack-and-Queue/stack.py)
41+
- [x] [Queue](Stack-and-Queue/queue.py)
4142

4243
This repository is for the references, anyone can feel free to use this.

Stack-and-Queue/queue.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Queue
3+
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes out first.
4+
'''
5+
6+
class Queue:
7+
8+
def __init__(self):
9+
self.queue = []
10+
11+
# Add an element
12+
def enqueue(self, item):
13+
self.queue.append(item)
14+
15+
# Remove an element
16+
def dequeue(self):
17+
if len(self.queue) < 1:
18+
return None
19+
return self.queue.pop(0)
20+
21+
# Display the queue
22+
def display(self):
23+
print(self.queue)
24+
25+
def size(self):
26+
return len(self.queue)
27+
28+
29+
q = Queue()
30+
q.enqueue(1)
31+
q.enqueue(2)
32+
q.enqueue(3)
33+
q.enqueue(4)
34+
q.enqueue(5)
35+
36+
q.display()
37+
38+
q.dequeue()
39+
40+
print("After removing:")
41+
q.display()
42+
'''
43+
[1, 2, 3, 4, 5]
44+
After removing:
45+
[2, 3, 4, 5]
46+
'''

0 commit comments

Comments
 (0)