0% found this document useful (0 votes)
1 views4 pages

Data Structure Ass 2 Docx

Uploaded by

aleenasyeda811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Data Structure Ass 2 Docx

Uploaded by

aleenasyeda811
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 02

Queue Operations

Semester: 04

Course Code: CC-213


Course Title: Data Structure
Due Date: 16 April, 2026
Submitted By: Syeda Aleena Zahra
Submitted To: Ma’am Saman Ashraf
Roll No: 11
Department: BSCS
Definition of Queue and FIFO
A queue is a linear data structure in which elements are inserted from one end called the rear and
removed from the other end called the front. It is an ordered list in which insertions are done at one
end which is known as the rear and deletions are done from the other end known as the front.
A good example of a queue is any queue of consumers for a resource where the consumer that came
first is served first.
The working principle of a queue is called FIFO (First In, First Out). This means that the element
which is inserted first will be removed first. There is no jumping or skipping allowed in a queue.
Real-life Example of FIFO

To understand this better, imagine you are standing in a line at a bakery. The first person who came
into the line will be served first, and the last person will have to wait. The same logic applies to a
queue in programming. For example, if we insert the values 10, 20, and 30 into a queue, the value 10
will be removed first, followed by 20, and then 30.

Types of Queues

Queue data structure can be classified into 3 types:

1. Simple Queue

A simple queue follows the FIFO (First In, First Out) principle.

 Insertion is allowed only at the rear (back).


 Deletion is allowed only from the front.

 Can be implemented using a linked list or a circular array.

When an array is used, we often prefer a circular queue, which is mainly an efficient array
implementation of a simple queue. It efficiently utilizes memory by reusing the empty spaces left
after deletion, avoiding wastage that occurs in a normal linear array implementation.

2. Circular Queue

A circular queue is an improved version of the simple queue. In this structure, the last position is
connected back to the first position, forming a circular shape. This allows the queue to reuse empty
spaces efficiently. For example, if elements are removed from the front, new elements can be added
in those freed spaces instead of leaving them unused. This makes circular queues more memory-
efficient
3. Priority Queue

A priority queue works differently compared to the previous two. In this type of queue, elements are
not served based on their arrival time but according to their priority. Elements with higher priority
are removed before those with lower priority. For instance, in a hospital emergency room, patients
with critical conditions are treated before others, regardless of when they arrived. This is a real-life
example of a priority queue.

Queue Using Array Implementation

A queue can be implemented using an array by maintaining two variables called front and rear. The
front points to the first element of the queue, while the rear points to the last element.

When a new element is added, the rear is increased, and the value is stored at that position. This
operation is called enqueue. When an element is removed, the front is increased, and the value at that
position is removed. This operation is called dequeue.

For example, suppose we have an empty queue. If we insert the values 5, 10, and 15, they will be
stored in sequence. When we remove an element, the value 5 will be removed first because it was
inserted first. The front pointer will then move to the next element.

C ++ program
#include <iostream>
using namespace std;

#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int value) {
if (rear == SIZE - 1) {
cout << "Queue Overflow" << endl;
} else {
if (front == -1) front = 0;
rear++;
queue[rear] = value;
cout << value << " inserted" << endl;
}
}

void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow" << endl;
} else {
cout << queue[front] << " removed" << endl;
front++;
}
}

int main() {
enqueue(5);
enqueue(10);
enqueue(15);
dequeue();
return 0;
}

This program clearly shows how elements are added and removed following the FIFO rule.

Use of Circular Queue

A circular queue is mainly used in situations where memory efficiency is important. In a simple
queue, once elements are removed, the space cannot be reused, which leads to wasted memory.
However, in a circular queue, this problem is solved by reusing the empty spaces.

One common use of a circular queue is in CPU scheduling, where processes are handled in a circular
manner so that each process gets equal time. Another example is in keyboard buffers, where data is
continuously stored and processed. It is also used in traffic systems and streaming applications,
where data flows continuously and needs efficient storage management.

You might also like