0% found this document useful (0 votes)
7 views5 pages

Queue

A queue is a data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. There are various types of queues including linear, circular, double-ended, and priority queues, each with different methods for insertion and deletion. Operations on queues include inserting elements, deleting elements, and displaying the contents, with specific conditions for checking if the queue is full or empty.

Uploaded by

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

Queue

A queue is a data structure that follows the First In First Out (FIFO) principle, where elements are added at the rear and removed from the front. There are various types of queues including linear, circular, double-ended, and priority queues, each with different methods for insertion and deletion. Operations on queues include inserting elements, deleting elements, and displaying the contents, with specific conditions for checking if the queue is full or empty.

Uploaded by

iqacgfgch2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Queue

“What is a queue?”

A queue is a special type of data structure (an ordered collection of items) where elements are
inserted from one end and elements are deleted from the other end. The end at which new elements
are added is called the rear and the end from which elements are deleted is called the front. Using
this approach, the First element Inserted is the First element to be deleted Out, and hence, queue is
also called First In First Out (FIFO) data structure.

“What are the different types of queues?”

Based on the method of insertion and deletion the queues are classified as shown below:
1. Linear queue (Ordinary queue)
2. Circular queue
3. Double ended queue (dequeue or qeque)
4. Priority queue

Linear queue (Ordinary queue)


A queue (linear queue or ordinary queue) is a special type of data structure (an ordered collection of
items) where elements are inserted from one end and elements are deleted from the other end. The
end at which new elements are added is called the rear and the end from which elements are deleted
is called the front. Using this approach, the First element Inserted is the First element to be deleted
out, and hence, queue is also called First In First Out (FIFO) data structure.

For example, consider the queue shown below having the elements 10, 50 and 20:

The items are inserted into queue in the order 10, 50 and 20. The variable q is used as an array
to hold these elements
Item 10 is the first element inserted. So, the variable first is used as index to the first element
Item 20 is the last element inserted. So, the variable rear is used as index to the last element
Two more items can be inserted into above queue.

Operations on queue
A queue (linear queue or ordinary queue) is a special type of data structure (an ordered collection of
items) where elements are inserted from one end and elements are deleted from the other end. The
end at which new elements are added is called the rear and the end from which elements are deleted
is called the front. Using this approach, the First element Inserted is the First element to be deleted
out, and hence, queue is also called First In First Out (FIFO) data structure.
For example, consider the queue shown below having the elements 10, 50 and 20:
The items are inserted into queue in the order 10, 50 and 20. The variable q is
used as an array to hold these elements
Item 10 is the first element inserted. So, the variable first is used as index to the
first element
Item 20 is the last element inserted. So, the variable rear is used as index to the
last element
Two more items can be inserted into above queue.
1 Insert into queue
Step 1: Consider the queue shown below. Can we insert any element into the queue?
No, it is not possible because queue is full.
Observe that whenever rear value is
equal to “QUEUE_SIZE – 1” insertion is
not possible. The code for this can be
written as shown below:
if (rear == QUEUE_SIZE - 1)
{
printf(“Queue is full\n”);
return;
}

Step 2: If the above condition is not


satisfied, it means the queue is not full
and an element can be inserted at the rear
end. Observe that item has to be inserted
after 30 at position 3. That is, before
inserting an item, we have to increment
rear by one. This can be achieved using
the statement:
rear = rear + 1;

Step 3: Now the item can be inserted at rear


position. This can be achieved by copying item
into q[rear] as shown below:
q[rear] = item;

Now, the complete function to insert an element from the rear end of the queue can
be written as shown below:

void Insert_Rear()
{
/* Check for overflow of queue */
if (rear == QUE_SIZE – 1)
{
printf(“Queue overflow\n”);
return;
}
/* Insert the item */
rear = rear + 1;
q[rear] = item;
}

2 Delete from queue

Step 1: Now, let us see “How to check whether queue is empty or not?”
So, it is observed from above two figures that if front is less than or equal to rear
some elements are present. Otherwise, that is, if front is greater than rear then queue
is empty. We can check for empty queue using the following statement:
if (front > rear) return -1; // Que is empty
Step 2: When above condition fails, we can delete an item from front end of queue.
For this to happen, we have to access and return the first element and increment value
of front by 1 as shown below:
return q[front++];
Now, the complete function to delete an element from the front end of the queue can
be written as shown below:

int Delete_Front()
{
if (front > rear) return -1;
return q[front++];
}
Note: The variables front, rear and q are global. So, they can be accessed in all the functions.

3 Display queue items


Step 1: Check for empty queue. This can be done using the following code:
if (front > rear)
{
printf(“Que is empty\n”);
return;
}
Step 2: If elements are present in queue control comes out of the above if statement.
Assume that the queue contains three elements as shown in figure.
The contents of queue can be displayed as shown below:
Output
printf(“%d\n”, s [0] ); 20
printf(“%d\n”, s [1] ); 25
printf(“%d\n”, s [2] ); 10
In general, we can use printf(“%d\n”, s [i] ); Note: i = front to rear
Now, the code takes the following form:
for (i = front; i <= rear; i++)
{
printf(“%d\n”, s[i]);
}
Now, the complete function can be written as shown below:
void Display()
{
int i;
/* If queue is empty */
if (front > rear)
{
printf(“Queue is empty\n”);
return;
}
/* Display contents of queue */
printf(“Contents of the queue\n”);
for (i = front; i <= rear; i++)
{
printf(“%d\n”, q[i]);
}
}

Queue implementation using arrays (static implementation of queues)


Lab program 6(Part B)

Types of Queues

1. Linear queue (Ordinary queue)


2. Circular queue
3. Double ended queue (dequeue or qeque)
4. Priority queue

2. Circular queue
In circular queue, the elements of a given queue can be stored efficiently in an array so as to “wrap
around” so that rear end of the queue is followed by the front of queue. The pictorial representation of
a circular queue and its equivalent representation using an array are given side by side in figure below:

3. Double ended queue (dequeue or qeque)


A Dequeue is a special type of data structure in which insertions are done from both ends and
deletions are done at both ends.

4. Priority queue

A queue in which we are able to insert items or remove items from any
position based on some priority is often referred to as a priority queue. Always an
element with highest priority is processed before processing any of the lower priority
elements. If the elements in the queue are of same priority, then the element, which
is
inserted first into the queue, is processed. Priority queues are used in job scheduling
algorithms in the design of operating system where the jobs with highest priorities
have to be processed first.
The priority queues are classified into two groups:
 Ascending priority queue: In an ascending priority queue elements can be
inserted
in any order. But, while deleting an element from the queue, only the smallest
element is removed first.
 Descending priority queue: In descending priority also elements can be
inserted in
any order. But, while deleting an element from the queue, only the largest element
is deleted first.

You might also like