Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

2.4 Queue Uing Array & Linked List.

Uploaded by

devaki meena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2.4 Queue Uing Array & Linked List.

Uploaded by

devaki meena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Widescreen

Presentatio
U19CS201- DATA STRUCTURES
MODULE 3 STACKS &
n QUEUES

G.G.SREEJA, AP/CCE
QUEUE ADT

Queue is a linear Data Structure in which the


operations are performed based on FIFO (First In
First Out) principle.

In a Queue always the Insertion operation is done


at “rear” and Deletion operation is done at “front”.

In a Queue sequence of elements entered into


the queue is same as the sequence of elements
leave the queue.
(contd)…
•Queue is a linear Data Structure in which the operations are
Definition performed based on FIFO (First In First Out) principle.

•rear – used to store the position of insertion


•front – used to store the position of deletion
•size – used to store the size of the queue
Fields •element – used to store the value to be inserted

•enQueue(element) – to insert into queue


•deQueue( ) – to delete from queue
Functions •display( ) – to display all elements in queue

•Rear >= size – Queue is full


•Front == rear – Queue is empty
Condition
Q u e u e r e p r e s e n
In queue, we access elements from both the ends for different
t a t io
reasons.n

As in stacks, queue can also be implemented using

-Array

-Linked List

A real-world example of queue can be a single-lane one-way road,


where the vehicle enters first, exits first. More real-world examples
can be seen as queues at the ticket windows and bus-stops
Q U E U E – P R I M I T I VE O
P E R A T IO N
Queue operatio ns m ay invo lve initi al izing or

S
d efining the queue, utilizing it, and then completely erasing it
from the memory
□ enqueue − add : store an item to the queue [ REAR END]
□ dequeue − remove : access an item from the queue [ FRONT END ]
□ peek − Gets the element at the front of the queue without removing it
□ IsFull − Checks if the queue is full [ REAR equals ARRAYSIZE-1 ]
□ IsEmpty − Checks if the queue is empty [ FRONT and REAR equals -1 ]
Implementation
#define size 10
void main()
{
int Q[size], front = -1, rear = -1,op,element;
void enQueue(int);
void deQueue();
void display();
printf(“Select your option: ”);
printf(“1: Insert\n2: Delete\n3:
Display\n”); scanf(“%d”,&op);
switch(op)
{
case 1: printf(“Enter element to be insert:
”); scanf(“%d”,&element);
enQueue(element);
break;
case 2: deQueue();
break;
case 3: display();
break;
When the compiler executes above code it allocates memory as
follows….

0 1 2 3 4 5 . . . SIZE-1

f = r = -1
f = front
Queue is EMPTY
r = rear
Insertion
enQueue( int element )
{
if( rear != size-1)
{
re a r + + ;
} Q [ r ea r ] =
elseelement; Queue is full !!!
printf(“Queue is full !!!”);
}

0 1 2 3 4 5 6 7 8 9

Q 10 20 30
r 40
Deletion
deQueue( )
{
if( front != rear )
{
front
r
} p intf(“\nElement removed
++;Queue is empty !!!
%:
else d”,Q[front]);
printf(“Queue is empty !!!)”;
}

0 1 2 3 4 5 6 7 8 9

Q 10 20 30 40
f r
Display
display( )
{
if( front != rear )
{
for(int i=front+1; i<=rear,
i++)
printf(“%d\t”,Q[i]);
}
else
printf(“Queue is empty !!!”);
0 1 2 3 4 5 6 7 8 9
}
Q 10 20 30 40
Enqueue Operation
Queues maintain two data pointers, front and rear
The following steps should be taken to enqueue insert data into a
queue Step 1 − Check if the queue is full

Step 2 − If the queue is full, produce overflow error and exit


Step 3 − If the queue is not full, increment rear pointer to point the next
empty space
Step 4 − Add data element to the queue location, where the
rear is pointing

Step 5 − return success


Enqueue-Algorithm

Step 1: IF REAR=MAX-1 Pseudo code for enqueue operation


WRITE “OVERFLOW” Procedure enqueue(queue, data)
Goto step 4 if queue is full
[END OF IF]
Step 2: IF FRONT=-1 and REAR=-1 return overflow
SET FRONT=REAR=0 endif
rear ← rear + 1
ELSE queue[rear] ← data
SET REAR=REAR+1
return true
[END OF IF]
end procedure
Step 4: SET QUEUE[REAR]=NUM
Step 5: EXIT
Routine to Insert an Element in
void EnQue ue (int X , Queue
Queue
Q) {
a
if ( Rear = = Arraysize - 1) // Insert an element X inside the Q
// Check for Q Overflow
print (" Full Queue !!!!. Insertion not possible");
// Special Case – Inserting an element X
else if (Front = = -1 && Rear = = - 1)
inside Empty Queue. So Increment
{ Front++;
both Front and Rear Pointers
Rear++;
Q [Rear] = X;
}
else { // Inserting an element X inside Existing Queue.
Rear++; So Increment only Rear Pointer
Q [Rear] =
X;

}
}
Dequeue Operation
Accessing data from the queue
The following steps are taken to perform dequeue operation

Step 1 − Check if the queue is empty


Step 2 − If the queue is empty, produce underflow error and exit
Step 3 − If the queue is not empty, access the data where front is
pointing
Step 4 − Increment front pointer to point to the next available
data element

Step 5 − Return success


Dequeue-Algorithm
Step 1: IF FRONT=-1 Pseudo code for dequeue operation

WRITE “UNDERFLOW” Procedure dequeue

ELSE if queue is empty


return underflow
SET VAL=QUEUE[FRONT]
end if
SET FRONT=FRONT+1 data = queue[front]
front ← front + 1
[END OF IF]
return true
Step 2: EXIT end procedure
Routine to delete an Element from a
// Access an element from Front of Q
v oid D e Qu eu e
Q ue u e
if ( Front = = - 1 && Rear = = - 1 )
( Queue Q ) {
// Check for Q Underflow
print (" Empty Queue !. Deletion not possible " );
else if ( Front = = Rear ) { // Special Case – If Q has only single element
X = Q [ Front ]; assign Q to empty state . Modify both Front
Front = - 1; and Rear Pointers
Rear = - 1;

}
else {
// Otherwise Increment only Front pointer
X = Q [ Front ];

Front = Front + 1 ;

} }
QUEUE – isFull and isEmpty
Implementing Queue using single dimension array, check for the rear
pointer to reach at
MAXSIZE-1 to determine that the queue is full
Algorithm - isFull Algorithm - isEmpty
begin procedure isEmpty
begin procedure isFull
if front and rear equals -1
if rear equals to MAXSIZE-1
return true return true
else else
return false return false
endif endif

end procedure end procedure


Queue using array-Animation
Linked list representation of queues
• In a linked list, every element has 2 parts, one that stores the data & another that
stores the address of the next element.

• The start pointer of the linked list is used as FRONT.


• Here, the another pointer called “REAR” is used. This will store the address of

the last element in the queue.

• All insertions will be done at the rear end and all the deletions will be done at the
front end.

• If FRONT=REAR=NULL, then it indicates that the queue is empty.


Operations on Linked queues

• A queue has 2 basic operations: insert & delete.


• The insert operation adds an element to the end of the queue & the delete operation

removes an element from the front or the start of the queue.

• Apart from this, there is another operation “peek” which returns the value of the 1st
element of the queue.
Insert Operation
Step 1: Allocate memory from the new node and name it as
PTR

Step 2: SET PTR->DATA = VAL

Step 3: IF FRONT = NULL

SET FRONT = REAR = PTR


SET FRONT->NEXT= REAR->NEXT =

NULL ELSE
SET REAR->NEXT =

PTR SET REAR =

PTR SET REAR ->

NEXT =

NULL [END OF IF]


(Contd)…
• In step 1, the memory is allocated for the new node.
• In step 2, the DATA part of the new node is initialized with the value

to be stored in the node.

• In step 3, we check if the new node is the 1st node of the linked queue.

• This is done by checking if FRONT=NULL.

• If this is the case, then the new node is tagged as FRONT as well as REAR.
• Also NULL is stored in the NEXT part of the node(which is also

the FRONT & the REAR node).

• However, if the new node is not the 1st node in the list, then it is added at
the REAR end of the linked queue (or the last node of the queue).
Delete Operation
Step 1: IF FRONT=NULL
Write
“Underflow”Goto

step 5

Step 2: SET PTR=FRONT

Step 3: SET FRONT= FRONT ->


NEXT

Step 4: FREE PTR

Step 5: END
(contd)…
• In step 1, we 1st check for the “underflow” condition.
• If the condition is true, then an appropriate message is displayed, otherwise we
use a pointer PTR that points to FRONT.

• In step 3, FRONT is made to pint to the node in sequence.

• In step 4, the memory occupied by PTR is given back to the free pool.
Queue using Linked List
Applications of

Queue
Serving requests on a single shared valuable resources like a
printer, CPU task scheduling etc.

❖ In real life, Call Center phone systems will use Queues, to hold
people calling them in an order, until a service representative is
free
❖ Handling of interrupts in real-time systems. The interrupts
are handled in the same order as they arrive, First come first
served
❖ Batch processing in operating system

❖ Job scheduling Algorithms like Round Robin


Algorithm uses Queue
Drawbacks of Queue (Linear Queue)
If the last position of the queue is occupied, it is not possible to
enqueue any more elements even though some positions are vacant
towards the front positions of the queue
Thank you

You might also like