- Name: Palak Soni
- PRN: 24070123069
- Class: ENTC A3
- Title: Implementation of Queue using Array in C++
To implement a Queue data structure using arrays in C++ and perform basic operations such as enqueue, dequeue, and display.
- To understand the working of the Queue (FIFO: First In, First Out) data structure.
- To implement Queue operations (insertion, deletion, display) using arrays.
- To study overflow and underflow conditions in queues.
- To analyze the limitations of a simple queue and understand its use cases.
- To strengthen the concepts of object-oriented programming (OOPs) using classes in C++.
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. The element that is inserted first is the one that gets removed first. This makes it different from stacks (which follow LIFO).
- Front: Refers to the index of the element that will be removed first (oldest element).
- Rear: Refers to the index where the next element will be inserted.
- Enqueue (Insertion): Adds an element to the rear of the queue.
- Dequeue (Deletion): Removes an element from the front of the queue.
- Display: Shows all the elements currently present in the queue.
- Sequential Access: Elements are processed in the order they arrive.
- Restricted Operations: Insertion happens only at the rear, deletion only from the front.
- Overflow Condition: When the queue is full, no more elements can be inserted.
- Underflow Condition: When the queue is empty, no element can be deleted.
- Simple Queue (Linear Queue): Insertion from rear, deletion from front.
- Circular Queue: Overcomes the wastage of space in linear queues.
- Priority Queue: Elements are dequeued based on priority, not order.
- Deque (Double Ended Queue): Insertion and deletion from both ends.
This experiment focuses on Simple Queue implementation using arrays.
Feature | Stack (LIFO) | Queue (FIFO) |
---|---|---|
Insertion | Top | Rear |
Deletion | Top | Front |
Order of Access | Last In, First Out | First In, First Out |
Use Cases | Function calls, Undo | Scheduling, Buffering |
- Start
- Initialize
front = -1
,rear = -1
. - Enqueue (value):
- If
rear == SIZE - 1
, print Overflow. - Else increment
rear
, insert element atarr[rear]
. - If
front == -1
, setfront = 0
.
- If
- Dequeue():
- If
front == -1 OR front > rear
, print Underflow. - Else delete element from
arr[front]
and incrementfront
.
- If
- Display():
- If queue is empty, print message.
- Else traverse from
front
torear
and display elements.
- Perform operations as per
main()
. - End.
- Implemented a Queue class with private members
arr
,front
, andrear
. - Defined enqueue, dequeue, and display functions.
- Tested the queue in the
main()
function with a set of operations. - Successfully demonstrated FIFO operation and handled overflow/underflow conditions.
- Object-Oriented Programming (OOPs): Encapsulation via class and methods.
- Data Structures: Queue using arrays.
- Control Structures: Conditional statements (
if-else
) to handle overflow/underflow. - Loops: For traversal during display.
- Macros:
#define SIZE 5
for queue size.
This experiment helped in understanding the queue data structure and its implementation using arrays in C++.
We observed:
- FIFO principle in action.
- How overflow occurs when the array is full.
- How underflow occurs when trying to remove from an empty queue.
- Practical usage of class-based encapsulation in C++.
Queues are widely used in real-life applications such as:
- CPU scheduling
- Printer queue management
- Buffering in networks
- Task scheduling systems