Queue Data Structure. Queue is FIFO(First In First Out) or LILO(Last In Last Out) data structure.
Example:
queue = []
Now, insert an element 1 at index 0
queue = [1]
Now, insert some more elements at index 0 So, every element which was placed on index 0 will push by 1 when you try to insert at index 0 queue will look like
queue = [5, 4, 3, 2, 1]
if i use pop() to queue it will return the last element in the list which is 1 hence FIFO proved
Queue is FIFO(First In First Out) or LILO(Last In Last Out) data structure.
The problem requires implementing a general queue data structure that supports basic operations such as enqueue, dequeue, checking if the queue is empty and display queue elements.
The queue is implemented using a deque (double-ended queue) from the collections module in Python, which allows for efficient appending and popping of elements from both ends.
Append and Pop operations, append element to the right side of the queue and pop element from the left side of the queue.
Example 1:
Input:
Q.enqueue(5)
Q.enqueue(6)
Q.enqueue(7)
Q.enqueue(8)
Q.display()
Output:
Queue elements: [5, 6, 7, 8]
Q.dequeue()
Q.display()
Output:
Queue elements: [6, 7, 8]
Q.dequeue()
Q.display()
Output:
Queue elements: [7, 8]
Q.dequeue()
Q.display()
Output:
Queue elements: [8]
Q.dequeue()
Q.display()
Output:
Queue is emptyQueue Data Structure : Producer Consumer Problem
This problem is a producer,consumer problem where producer thread is producing data whereas consumer
thread is consuming the data which has already produced.
Queue Data Structure : Print Binary Numbers From 1 to 10 Using Queue
The binary number In mathematics and in computing systems, a binary digit, or bit, is the smallest unit of data.
declare two different stings
s1 = "0"
s2 = "1"
enqueue() element "1" to queue and print front() of queue which returns [-1] element from the queue.
Now, enqueue() front + s1, and front + s2 to th queue.
Now dequeue() the element from queue which removes the first entered element.
print the front of the queue
repeat above steps to nth times.
Output:
The 0 number is : 1
The 1 number is : 10
The 2 number is : 11
The 3 number is : 100
The 4 number is : 101
The 5 number is : 110
The 6 number is : 111
The 7 number is : 1000
The 8 number is : 1001
The 9 number is : 1010
Notice a pattern above. After 1 second and third number is 1+0 and 1+1. 4th and 5th number are second number (i.e. 10) + 0 and second
number (i.e. 10) + 1.
For more clarification, please take a look on my code.
A Circular Queue is a linear data structure that follows the FIFO (First In First Out) principle, but unlike a traditional queue, it wraps around upon reaching the end, forming a circle. This structure helps avoid wastage of space that occurs in linear queues after multiple enqueue and dequeue operations.
The circular queue is implemented using a fixed-size list in Python, along with two pointers:
front: Points to the element at the front of the queue.rear: Points to the most recently inserted element at the rear.
Example 1:
Input:
Q.enqueue(5)
Q.enqueue(6)
Q.enqueue(7)
Q.enqueue(8)
Q.display()
Output:
Queue elements: [5, 6, 7, 8]
Q.dequeue()
Q.display()
Output:
Queue elements: [6, 7, 8]
Q.size_of_queue()
Output:
3
Q.peek()
Output:
8This implementation of a circular queue provides an efficient way to manage a fixed-size queue with wrap-around behavior, allowing for continuous use of the allocated space.
-
Functionalities
- enqueue(item): Adds an item to the queue.
- dequeue(): Removes and returns the front item from the queue.
- peek(): Returns the front item without removing it.
- display(): Shows the current state of the queue.
- size_of_queue(): Returns the number of elements currently in the queue.
- is_empty(): Checks whether the queue is empty.
-
Why Circular Queue Approach?
- Efficient Space Usage: Reuses freed-up space after dequeues, unlike linear queues.
- Fixed Memory: Operates within a fixed-size array, making it space-predictable.
- Real-world Use Cases: Circular buffers are common in OS scheduling, streaming data buffers, etc.
- Avoids Shifting: No need to shift elements manually like in arrays or linear queues.
-
Problem Solving Pattern
- Two Pointer Technique: Used to manage front and rear positions.
- Modular Arithmetic: Used to wrap around the index using
(index + 1) % size. - Simulation: Mimics real queue behavior using Python arrays.
-
Let's walk through the operations using the following input sequence:
Q.enqueue(5) Q.enqueue(6) Q.enqueue(7) Q.enqueue(8) Q.display() Q.dequeue() Q.display() Q.size_of_queue() Q.peek()
-
Assume a queue of size 5.
-
Initial State
Operation front rear Queue State Output / Notes Init -1 -1 [None, None, None, None, None] Empty queue initialized -
Enqueue Operations
Operation front rear Queue State Output enqueue(5) 0 0 [5, None, None, None, None] Enqueued: 5 enqueue(6) 0 1 [5, 6, None, None, None] Enqueued: 6 enqueue(7) 0 2 [5, 6, 7, None, None] Enqueued: 7 enqueue(8) 0 3 [5, 6, 7, 8, None] Enqueued: 8 -
Display Queue
Operation Queue State Output display() [5, 6, 7, 8, None] Circular Queue: [5, 6, 7, 8, None] -
Dequeue Operation
Operation front rear Queue State Output dequeue() 1 3 [None, 6, 7, 8, None] Dequeued: 5 -
Display After Dequeue
Operation Queue State Output display() [None, 6, 7, 8, None] Circular Queue: [None, 6, 7, 8, None] -
Size and Peek
Operation front rear Output size_of_queue() 1 3 3 peek() 1 3 6
| Operation | Time Complexity | Explanation |
|---|---|---|
| enqueue() | Direct index update using modulo arithmetic. | |
| dequeue() | Removes element at front and adjusts pointer. |
|
| peek() | Constant time access to the front element. | |
| display() | Traverses up to n elements depending on fill state. |
|
| size_of_queue() | Calculated using formula without iteration. |
| Resource | Space Complexity | Explanation |
|---|---|---|
| Queue Storage | Fixed array of size n. |
|
| Auxiliary Space | Constant additional space for pointers and helper methods. |