Skip to content

Commit 4b7f070

Browse files
committed
Queue code added
1 parent 81709ec commit 4b7f070

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

Data Structure/Queue/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Queue
2+
3+
**Queue** is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
4+
5+
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
6+
7+
![Queue](queue.svg)
8+
9+
**Basic Operations:**
10+
11+
Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory.. Apart from these basic stuffs, a queue is used for the following two primary operations −
12+
13+
- **enqueue()** − add (store) an item to the queue.
14+
- **dequeue()** − remove (access) an item from the queue.
15+
16+
To use a queue efficiently, we need to check the status of queue as well. For the same purpose, the following functionality are available too −
17+
18+
- **peek()** − get the first data element of the queue, without removing it.
19+
- **isEmpty()** − check if queue is empty.
20+
21+
22+
#### Complexity Analysis
23+
- Insertion - O(1)
24+
- Deletion - O(1) (If implemented with array, then deletion will take O(n) for shifting elements to rearrange the Queue)
25+
- Access - O(n)
26+
- Search - O(n)
27+
28+
### More on this topic
29+
- https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
30+
- https://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
31+
- https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues

Data Structure/Queue/queue.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
""" Queue implementation using Python List """
2+
3+
class Queue(object):
4+
5+
def __init__(self):
6+
self.items = []
7+
8+
def isEmpty(self):
9+
return (self.items == [])
10+
11+
def enqueue(self, item):
12+
self.items.append(item)
13+
14+
def peek(self):
15+
return self.items[0]
16+
17+
def dequeue(self):
18+
if self.isEmpty():
19+
return None
20+
return self.items.pop(0) #pops the first element from the list

Data Structure/Queue/queue.svg

5.49 KB
Loading

Data Structure/Queue/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from queue import Queue
2+
3+
queue = Queue()
4+
queue.enqueue(10)
5+
queue.enqueue(15)
6+
7+
print(queue.isEmpty())
8+
print(queue.peek())
9+
print(queue.dequeue())
10+
print(queue.dequeue())
11+
print(queue.dequeue())
12+
print(queue.isEmpty())

0 commit comments

Comments
 (0)