Sie sind auf Seite 1von 11

Data Structures and Algorithms

CS F211
Vaibhav Soni
Assistant Professor
BITS Pilani Department of Computer Science and Information Systems
Pilani Campus BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Agenda: Queue ADT


Introduction
• Queue is a linear data structure in which the operations are
performed based on FIFO (First-In-First-Out) principle. Hence,
element inserted first will be deleted first.
• In queue, data elements are inserted at one end, called the rear
and deleted from another end, called the front of the list.
• Two basic operations:
– “Enqueue” operation is used to insert an element at the rear.
– “Dequeue” operation is used to delete an element from the
front.

Department of CSIS, BITS Pilani (Pilani Campus)


Introduction (cont.)
• For example: line in a library, the first one in the line is the
first one to be served. Just like a queue.

Department of CSIS, BITS Pilani (Pilani Campus)


Introduction (cont.)
• Auxiliary queue operations:
– front(): returns the element at the front without removing it
– size(): returns an integer value that indicates the number of
elements stored in the queue
– isEmpty(): returns a Boolean value that indicates whether the
queue is empty
• Applications:
– In real life scenario, Call Center phone systems uses Queues to
hold people calling them in an order, until a service
representative is free
– Access to shared resources (e.g., printer)
– Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e., FCFS
Department of CSIS, BITS Pilani (Pilani Campus)
Algorithms for Enqueue and
Dequeue Operations
Enqueue operation:
• Step 1 - Check whether queue is FULL. (rear == MAX-1)
• Step 2 - If it is FULL, then display "Queue is FULL…".
• Step 3 – else check whether (front == -1), if it is true, then set both front
and rear to 0 and set queue[rear] = value. Otherwise, increment rear value
by one (rear++) and set queue[rear] = value.
Dequeue operation:
• Step 1 - Check whether queue is EMPTY. (front == -1 ||front == rear + 1)
• Step 2 - If it is EMPTY, then display "Queue is EMPTY…".
• Step 3 - else display queue[front] as deleted element and increment the
front value by one (front ++) Then check whether ( front == rear +1 ), if it
TRUE, then set both front and rear to '-1' ( front = rear = -1 ).

Department of CSIS, BITS Pilani (Pilani Campus)


Queue variations
The standard queue data structure has the following variations:
• Double-ended queue (DEQUE):
– In a double-ended queue (Deque), elements can be inserted and
deleted from both the front and back of the queue.
• Circular queue:
– A circular queue is an improvement over the
standard queue structure, which overcomes
some drawback of the normal queue. It is
also called ‘Ring Buffer’.
– In a normal Queue, we can insert elements
until queue becomes full. But once queue
becomes full, we can not insert the next
element even if there is a space in front of
queue.

Department of CSIS, BITS Pilani (Pilani Campus)


/* Insert an element to the CQueue*/

void enQueue(int item)


{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Overflow");
return;
}
if(front == -1)
{
front = 0; rear = 0;
}
else
{
rear = (rear + 1) % MAX;
}
cqarray [rear] = item ;
}
/* Delete an element from the CQueue*/

int deQueue()
{
if(front == -1)
{
printf("Underflow");
return ;
}

int element = cqarray[front]);


if(front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % MAX;
}
return element;
}
Query…?

Das könnte Ihnen auch gefallen