You are on page 1of 4

Data Structures & Algorithms / Data Structures

Hamdard University

LAB SESSION 05
QUEUES AND PRIORITY QUEUES
Objectives:

To understand the basic concept of Queuesand its operations.

To understand the ways for implementing Queues.

To understand operations on Queues

To understand the concept of Priority Queues.

Definition:
A queue is a special type of linear structure. It holds a series of items for processing on a first element on a first
in first out basis. Elements can only be inserted to the back of a queue. Only the front elements can be accessed
and modified.
A queue is a FIFO (First In First Out) Structure. In Queue inserting an element is known as ENQUEUE and is done
at the REAR end of the queue. Deletion of an element is called DEQUEUE and is done at the FRONT end. An
element in a queue is called an ITEM. The number of elements that a queue can accommodate is termed as
LENGTH.

Representation of Queues:
There are two ways to represent a queue in memory:

Using an array

Using a linked list

Operation on Queues:
Different operations can be performed on queues like:

Enqueue

Count No of items in Queue

Dequeue

Display Queue

Data Structures & Algorithms / Data Structures


Hamdard University

IsFull()

IsEmpty()

etc

Algorithm for Enqueue:


Let front and rear be the two ends of the queue ,'item' be the item to be added and qType represent
the queue data type
1. Create a new node
ptr=(qType*)malloc(sizeof(qType))
2. Assign the item to info
set ptr->info = item
3. Adjust ptr
set ptr->next= NULL
4. Check for existing queue
if ( rear==NULL)
{
set rear=ptr
set front=ptr
}
else goto 5.
5. Adjust rear pointer
Set rear -> next =ptr
Set rear= ptr
6.exit

Data Structures & Algorithms / Data Structures


Hamdard University
Algorithm for Dequeue
1. Check if

queue is Empty
if (front==NULL)
display "Stack Empty " and exit else goto 2

2. Set temp=front
3. Adjust fron
front=front->next
4. Display temp->info
5.Free(temp)
6.Exit

Data Structures & Algorithms / Data Structures


Hamdard University

Lab Task
1) Write and execute the example given in priority queue, Check if it is ascending priority queue or
descending priority queue. Write the code for the vice versa. Also implement Enqueue () and Dequeue()
methods.
2) Implement the priority queue using pointers. Also implement Enqueue () and Dequeue() methods.
3) Write a code that searched for first three highest elements in the queue.
4) Write a code that inputs five elements in a queue and displays the average of those elements.
5) Write a program to insert ten names into a priority queue. Print all the names from the priority queue
that have the top priority.

You might also like