Chapter-7 QUEUESds
Chapter-7 QUEUESds
QUEUES
The linear lists and linear arrays allow inserting and deleting elements at any place in the list i.e.
at the beginning, at the end, or in the middle. There are certain frequent situations when one
wants to restrict insertions and deletions so that they can take place only at the beginning or at
the end of the list, not in the middle. One of the data structures that are useful in such situations
is a Queue.
QUEUE
A Queue is a linear list of elements in which data can be inserted at one end, called the rear, and
deleted from the other end, called the front. Queues are also called first in–first out (FIFO) lists,
since the first element in a queue will be the first element out of the queue.
Examples
1) Array representation
The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.
The condition FRONT = NULL will indicate that the queue is empty.
FRONT : 1
REAR : 4
1 2 3 4 5 6 7 … N
The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.
QINSERT(QUEUE,N,FRONT,REAR, ITEM)
ELSE
ELSE
} // End of If
6) Return.
QDELETE(QUEUE,N,FRONT,REAR,ITEM)
Return
ELSE
Else
}// End of If
4) Return.
Complexity
int data;
};
QUEUE *front;
QUEUE *rear;
int count;
Create queue
Enqueue
Deletequeue
Queuefront
Queuerear
Empty queue
Full queue
Queue count
Destroy queue
Create queue
Create queue allocates a node for the queue header. It then initializes the front and rear pointers
to null and sets the count to zero. If overflow occurs, the return value is null. If the allocation is
successful, it returns the address of the queue head.
/ / Local Declarations
QUEUE *queue;
2) // Is memory available
If (queue)
queue àcount = 0 ;
} // End of if
return queue;
} // End of createqueue
Enqueue
Enqueue operation is used to insert an element into the queue. When we insert data into a queue
with data in it, we must point both the link field in the last node and the rear pointer to the new
node.
QUEUE *newnode;
if (queue àcount==0)
queueàrear = newnode;
newnodeànext = null;
else
} / * End of if * /
return 1 ;
} / * End of enqueue * /
Complexity
Deletequeue
While deleting an element from a queue we must first ensure that the queue contains data. Given
that there are data to be deleted, we pass the data back through the parameter list and then set the
front pointer to the next item in the queue. The node that is deleted is recycled.
1) if (queueàcount == 0)
return 0;
if (queue àcount == 1)
else
(queue àcount)--;
free(item);
return 1;
} / * End of Deletequeue * /
Complexity
Queuefront
// This algorithm retrieves the data at the front of the queue without changing the queue contents.
if (queueàcount == 0)
return 0;
else
return(item);
} / *end of else */
} / * end of queueFront * /
Queuerear
// This algorithm retrieves the data at the rear of the queue without changing the queue contents.
If (queueàcount ==0)
return 0;
else
return (item);
} / *end of else */
} / * end of queueRear * /
QueueEmpty
It simply uses the count in the queue header to determine if the queue is empty or contains data.
return(queueàcount == 0);
} / * End of emptyqueue * /
FullQueue
Checks to see if a queue is full. If the queue is full if memory cannot be allocated for another
node.
// This algorithm checks to see if a queue is full. The queue is full if memory cannot be
allocated // for another node.
QUEUE *temp;
free (temp);
return 0;
else
return 1;
Queue count
Queue count returns the number of elements in the queue. Queue count simply returns the count
found in the queue head node.
return (queueàcount);
Destroy Queue
The Destroy Queue operation deletes all the elements in the queue and frees each of their nodes.
// This algorithm deletes all data from a queue, then deletes and recycles queue head pointer.
QUEUE * queue;
1) if (queue)
temp = QUEUEàfrontàinfo;
QUEUEàfront = QUEUEàfrontànext;
free(temp);
} // End of while
} // End of if
return NULL;
} // End of destroyQueue
TYPES OF QUEUES
Queues are classified into three types
Circular queues
Dequeue (Double Ended Queue)
Priority queues
CIRCULAR QUEUES
A circular queue is a linear collection of elements in which the front and rear elements will be in
successive locations.
1) Array representation
If we are representing the queue using arrays, then a queue with n elements starts from index 0
and ends at n – 1.The first element in the queue is at index 0 and the last element will be at index
n – 1.
The queue will be maintained by a linear array QUEUE and two pointer variables: FRONT,
containing the location of the front element of the queue and REAR, containing the location of
the rear element of the queue.
CQINSERT(Front,Rear,Queue,N,Y)
IF REAR = N
then REAR = 1;
If Front = Rear
return 0;
else
3) Rear = Rear + 1;
/ * Insert element * /
Queue[Rear] = y;
If Front = 0
then FRONT = 1;
return 0;
Complexity
Algorithm cqdelete(Front,Rear,Queue,N,Y)
If Front = 0
then print(“Underflow”);
2) / * Delete element * /
Y=Queue[Front];
then Front = 1
else
Front =Front + 1
return(Y);
QUEUEàrear = newnode;
Complexity
free(QUEUEàfront);
free(item);
Complexity
A Dequeue (Double ended queue) is an abstract data type similar to queue, where addition and
deletion of elements are allowed at both the ends. A Dequeue can be implemented using Arrays
or linked lists.
1) If (front == Queue_Length – 1)
return 0;
2) Rear = Rear + 1;
3) dq[rear] = y;
4) If (rear == - 1)
Rear = 0;
Complexity
1) If Front == -1
Print(“Queue empty”);
2) else
3) If (front == rear)
Front = rear = -1
Else
4) Front = front + 1;
1) If (front ==0)
return 0;
else
2) Front = front – 1
3) dq[front] = y;
4) If (Front = - 1) then
Front = 0
Complexity
1)If rear == - 1
else
2) If (front == rear)
Front = rear = - 1
else
3) Rear = rear – 1;
4) return(item = dq[rear]) ;
Complexity
In Input restricted double ended queue, the insertion operation is performed only at one end and
deletion operation is performed at both the ends.
1) If (front == Queue_Length – 1)
return 0;
else
2) Rear = Rear + 1;
3) dq[rear] = y;
Rear = 0;
1) If Front == -1
Print(“Queue empty”);
2) else
Return dq[front];
3) If (front == rear)
Front = rear = -1
Else
4) Front = front + 1;
else
2) If (front == rear)
Front = rear = - 1
else
3) Rear = rear – 1;
4) return(item = dq[rear]) ;
In Output restricted double ended queue, the deletion operation is performed only at one end and
insertion operation is performed at both the ends.
1) If (front == Queue_Length – 1)
return 0;
else
2) Rear = Rear + 1;
3) dq[rear] = y;
4) If (rear == - 1)
Rear = 0;
1) If (front ==0)
return 0;
else
2) Front = front – 1
3) dq[front] = y;
4) If (Front = - 1) then
Front = 0
1) If Front == -1
Print(“Queue empty”);
2) else
Return dq[front];
3) If (front == rear)
Front = rear = -1
4) Front = front + 1;
// Local Declarations
QUEUE *newnode;
newnodeàdata = info;
If (QUEUEàcount == 0)
else
5) newnodeàlink = NULL;
return 1 ;
} End of if
} // End of enqueue
Complexity
// Local Declarations
QUEUE *temp;
If (QUEUE count = 0)
return 0;
temp= QUEUEfront;
(QUEUE àcount)--;
free(temp);
return 1;
} // End of Deletequeue
Complexity
// Local Declarations
QUEUE *newnode;
2) // Allocate new node and move data into the new node
newnodeàdata = info;
If (QUEUEàcount == 0)
else
return 1 ;
} // End of if
} // End of enqueue
Complexity
// Local Declarations
QUEUE *temp;
If (QUEUE count = 0)
return 0;
else
temp= QUEUErear;
(QUEUE àcount)--;
free(temp);
return 1;
} // End of Deletequeue
Complexity
// Local Declarations
QUEUE *newnode;
2) // Allocate new node and move data into the new node
newnodeàdata = info;
If (QUEUEàcount == 0)
else
5) newnodeàlink = NULL;
return 1 ;
} End of if
} // End of enqueue
// Local Declarations
QUEUE *temp;
If (QUEUE count = 0)
return 0;
else
temp= QUEUEfront;
(QUEUE àcount)--;
free(temp);
return 1;
// Local Declarations
QUEUE *temp;
If (QUEUE count = 0)
else
temp= QUEUErear;
(QUEUE àcount)--;
free(temp);
return 1;
} // End of Deletequeue
// Local Declarations
QUEUE *newnode;
2) // Allocate new node and move data into the new node
newnodeàdata = info;
If (QUEUEàcount == 0)
else
5) newnodeàlink = NULL;
return 1 ;
} End of if
} // End of enqueue
// Local Declarations
QUEUE *newnode;
2) // Allocate new node and move data into the new node
newnodeàdata = info;
If (QUEUEàcount == 0)
else
return 1 ;
} // End of if
// Local Declarations
QUEUE *temp;
If (QUEUE count = 0)
return 0;
else
temp= QUEUEfront;
(QUEUE àcount)--;
free(temp);
return 1;
} // End of Deletequeue
PRIORITY QUEUES
A priority queue is a queue in which each element has been assigned a priority such that the
order in which elements are deleted and processed comes from the following rules:
2) Two elements with the same priority are processed according to the order in which they were
added to the queue.
1) Array representation
Assume there are a fixed number of priorities; say M .Create an array of M queues.
A multiqueue implementation using a single dimensional array with ‘m’ elements is depicted in
the above figure. Each queue has ‘n’ elements which are mapped to a linear array of ‘m’
elements.
Algorithm insertpriorityqueue(i, x)
// This algorithm inserts an item with priority i into the ith entry of the array.
/ * Add x to queue i * /
int i,x;
1) ++rear[i];
2) if(rear[i] == front[i+1])
else
3) rear[i] = rear[i] + 1;
4) mqueue[rear[i]] = x;
Complexity
Algorithm delmq(i,x)
int i,x;
1) if(front[i] == rear[i])
printf(“Queue is empty”);
return 0;
2) x = mqueue[front[i]];
3) front[i] = front[i] – 1;
return x;
Complexity
One way to maintain a priority queue in memory is by means of a one-way list as follows :
a) Each node in the list will contain 3 items of information: an information field INFO, a priority
number PRN and a link number LINK.
ii) When both have the same priority but X was added to the list before Y.
The order in the one-way list corresponds to the order of the priority queue. The lower the
priority number, the higher the priority. The main property of the one-way list representation of a
priority queue is that the element in the queue that should be processed first always appear at the
beginning of the one-way list.
if(temp==NULL)
newnode=(NODE *)malloc(sizeof(NODE));
newnode->data = value;
newnode->priority = priority;
newnode->link=NULL;
if (QUEUEàrear == NULL)
QUEUEàrear = newnode;
else
newnode->link = QUEUEàfront;
QUEUEàfront = newnode;
else
QUEUEàrear->link = newnode;
QUEUEàrear = newnode;
else
7) current=current->link;
8) for(int i=0;i<pos;i++)
9) if (currentàlink == pos)
newnode->link = current->link;
current->link = newnode;
Complexity
Algorithm delete(QUEUE *queue, QUEUE *front, QUEUE *rear, int item, int priority)
QUEUE *queue;
1) if(QUEUEcount=0)
return 0;
item = QUEUEfront->info;
temp = QUEUEfront;;
QUEUEfront= QUEUErear=NULL;
QUEUEfront= QUEUEfrontlink;
free(temp);
return 1;
Complexity
APPLICATIONS OF QUEUES
Queuing Theory
Queuing theory is a field of applied mathematics that is used to predict the performance of
queues.
Queue types
Service - The service is any activity needed to accomplish the required result.
Affected factors
Arrival rate - The rate at which customers arrive in the queue for service.
Service time - The average time required to complete the processing of a customer request.
Simulation is the use of one system to initiate the behavior of another system. Computer
simulation uses the steps of a program to initiate the behavior of system under study. In
computer simulation the objects being studied are represented as data, data structures whose
entries describe the properties of the objects. Actions in the system are represented as operations
on the data, and the rules describing these actions are translated into computer algorithms.
QUEUES are important data structures for use in computer simulations.
APPLICATION – 1
We build a model of a single-server queue. The store is open 8 hours per day, 7 days a week. To
simulate a day, we build a model that runs for 480 minutes (8 hours × 60 minutes). The
simulation uses a digital clock that events start and stop in 1-minute intervals.
Events
• newCustomer
• svcFree (svcStart)
• svcComplete
Algorithm customersimulation
4) increment Clock
5) PrintStats(runStats)
2) If (new customer)
increment custNum
enqueue(queue, custData)
// This algorithm determines if the server is idle and if so starts servicing the new customer.
// Server is idle.
dequeue(queue,custData)
queueCount(queue))
Algorithm printStats(stats)
APPLICATION -2
To simulate airport traffic control for a small but busy airport assuming the following
specifications.
There is only one runway for both landing and take off.
In each unit time, only one plane can land or take off but not both.
Planes arrive and take off at random time, so that at any given unit, the runway may be
idle or a plane may be landing or taking off.
Number of planes waiting to take off or land should not exceed a certain fixed limit.
Landing planes should have high priority than Taking-off plane.
After running for some period, the program should print statistics such as:
Number of planes processed,
Number of planes landed, and
Number of planes took off
Number of planes refused
Average landing and take-off waiting times, and
Average run-way idle time.
Algorithm
else
8) If landing queue is not empty process next landing plane and increment count of planes
landed
else
9) if takeoff queue is not empty, process next plane and increment count of take off planes
else