0% found this document useful (0 votes)
11 views

UNIT - 1 Queues

A queue is a linear data structure that follows the FIFO principle, where insertion (ENQUEUE) occurs at the rear end and deletion (DEQUEUE) occurs at the front end. It can be represented using arrays or linked lists, with specific conditions for empty and full states. Additionally, circular queues allow for efficient use of space by wrapping around the ends of the array.

Uploaded by

hemanthnk04
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)
11 views

UNIT - 1 Queues

A queue is a linear data structure that follows the FIFO principle, where insertion (ENQUEUE) occurs at the rear end and deletion (DEQUEUE) occurs at the front end. It can be represented using arrays or linked lists, with specific conditions for empty and full states. Additionally, circular queues allow for efficient use of space by wrapping around the ends of the array.

Uploaded by

hemanthnk04
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/ 11

QUEUES

Queue is a linear Data structure.


Definition: Queue is a collection of homogeneous data elements, where insertion and
deletion operations are performed at two extreme ends.
 The insertion operation in Queue is termed as ENQUEUE.
 The deletion operation in Queue is termed as DEQUEUE.
 The number of elements that a queue can accommodate is termed as LENGTH of the
Queue.
 In the Queue the ENQUEUE (insertion) operation is performed at REAR end and
DEQUEUE (deletion) operation is performed at FRONT end.
 Queue follows FIFO principle. i.e. First In First Out principle. i.e. a item First inserted
into Queue, that item only First deleted from Queue, so queue follows FIFO principle.

DEQUEUE ENQUEUE

FRONT REAR
Schematic Representation of Queue
Representation of Queue
A Queue can be represented in two ways
1. Using arrays
2. Using Linked List

1. Representation of Queue using arrays


A one dimensional array Q[0 – N-1] can be used to represent a queue.
1 2 3

N-2

N-1

N
. . - - - . .

Array representation of Queue


FRONT REAR

In array representation of Queue, two pointers are used to indicate two ends of Queue.
The above representation states as follows.
1. Queue Empty condition
Front = -1 and Rear = -1
Front = NULL and Rear = NULL
2. Queue Full condition
Rear = N - 1 where N is the size of the array we are taken
3. Queue conta ins only on e element
Front = = Rear
4. Number of items in Queue is
Rear – Front + 1
Queue overflow: Trying to perform ENQUEUE (insertion) operation in full Queue is known
as Queue overflow.
Queue overflow condition is Rear == N - 1
Queue Underflow: Trying to perform DEQUEUE (deletion) operation on empty Queue is
known as Queue Underflow.

Queue Underflow condition is Front = -1 and Rear = -1

ADT QUEUE Its Operations:


The abstract datatype is special kind of datatype, whose behavior is defined by a set of values
and set of operations. The keyword “Abstract” is used as we can use these datatypes, we can
perform different operations. But how those operations are working that is totally hidden from
the user. The ADT is made of with primitive datatypes, but operation logics are hidden.
The most fundamental operations in the queue ADT include: enqueue(), dequeue(), peek(), isFull(),
isEmpty().
Operation on Queue
1. ENQUEUE : To insert element in to Queue
2. DEQUEUE : To delete element from Queue
3. Status : To know present status of the Queue
Algorithm Enqueue(item)
Input: item is new item insert in to queue at rear end.
Output: Insertion of new item queue at rear end if queue is not full.
1. if(rear = = N - 1 )
a) print(queue is full, not possible for enqueue operation)
2. else
i) if(front = = -1 and rear = = -1) /* Q is Empty */
a) rear=rear+1
b) Q[rear]=item
c) Front = 0
ii) else
a) rear=rear+1
b) Q[rear]=item
iii) end if
3.end if
End Enqueue

While performing ENQUEUE operation two situations are occur.


1. if queue is empty, then newly inserting element becomes first element and last
element in the queue. So Front and Rear points to first element in the list.
2. If Queue is not empty, then newly inserting element is inserted at Rear end.

Algorithm Dequeue( )
Input: Queue with some elements.
Output: Element is deleted from queue at front end if queue is not empty.
1. if(front = = -1 and rear = = -1 )
a) print(Q is empty, not possible for dequeue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) item=Q[front]
b) front=-1
c) rear=-1
ii) else
a)item=Q[front]
b)front=front+1
iii) end if
iv) print(deleted item is item)
3. end if
End Dequeue

While performing DEQUEUE operation two situations are occur.


1. if queue has only one element, then after deletion of that element Queue becomes
empty. So Front and Rear becomes 0.
2. If Queue has more than one element, then first element is deleted at Front end.

Algorithm Queue_Status( )
Input: Queue with some elements.
Output: Status of the queue. i.e. Q is empty or not, Q is full or not, Element at front end and
rear end.
1. if(front = = -1 and rear = = -1)
a) print(Q is empty)
else if (rear = = N- 1 )
b) print(Q is full)
2. else
i) if(front = = rear)
a) print(Q has only one item)
ii) else
a) print(element at front end is Q[front])
b) print(element at rear end is Q[rear])
iii) end if
3. end if
End Queue_Status

2. Representation of Queue using Linked List


 Array representation of Queue has static memory allocation only.
 To overcome the static memory allocation problem, Queue can be represented using
Linked List.

 In Linked List Representation of Queue, Front always points to First node in the
Linked List and Rear always points to Last node in the Linked List.

The Linked List representation of Queue stated as follows.


1. Empty Queue condition is
Front = NULL and Rear = NULL or header.link == NULL
2. Queue full condition is not available in Linked List representation of Queue, because in
Linked List representation memory is allocated dynamically.
3. Queue has only one element
Front = = Rear

Operation on Linked List Representation of Queue


1. ENQUEUE : To insert element in to Queue
2. DEQUEUE : To delete element from Queue
3. Status : To know present status of the Queue

Algorithm Enqueue _LL(item)


Input: item is new item to be insert.
Output : new item i.e new node is inserted at rear end.
1. new=getnewnode()
2. if(new = = NULL)
a) print(required node is not available in memory)
3. else
i) if(front = = NULL and rear = = NULL) /* Q is EMPTY */
a) header.link=new
b) new.link=NULL
c) front=new
d) rear=new
e) new.data=item
ii) else /* Q is not EMPTY */
a) rear.link=new /* 1 */
b) new.link=NULL /* 2 */
c) rear=new /* 3 */
d) new.data=item
iii) end if
4. end if
End_Enqueue_LL
While performing ENQUEUE operation two situations are occur.
1. if queue is empty, then newly inserting element becomes first node and last node in
the queue. So Front and Rear points to first node in the list.
2. If Queue is not empty, then newly inserting node is inserted at last.

header N1 N2 N3 new

X X
2
Front Rear NULL
3

Rear
Before ENQUEUE

header N1 N2 N3 new

X X

Rear
After ENQUEUE

1. Previous last node link part is replaced with address of new node.
2. Link part of new node is replaced with NULL, because new nodes becomes the last
node.
3. Rear is points to last node in the list. i.e. newly inserted node in the list.
Algorithm Dequeue_LL( )
Input: Queue with some elements
Output: Element is deleted at front end if queue is not empty.
1.if(front= =NULL and rear = =NULL)
a) print(queue is empty, not possible to perform dequeue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) header.link = NULL
b) item=front.data
c) front=NULL
d) rear=NULL
b) else /* Q has more than one element */
a) header.link = front.link /* 1 */
b) item=front.data
c) free(front)
d) front=header.link /* 2 */
c) end if
d) print(deleted element is item)
3. end if
End_Dequeue_LL

While performing DEQU EUE operation two situations are occur.


1. if queue has only one element, then after deletion of that element Queue becomes
empty. So Front and Rear points to NULL.
2. If Queue has more than one element, then first node is deleted at Front end.
1. Link part of the header node is replaced with address of second node. i.e. address of
second node is available in link part of first node.
2. Front is set to first node in the list.
Algorithm Queue_Status_LL
Input: Queue with some elements
Output: Status of the queue. i.e. Q is empty or not, Q is full or not, Element at front end and
rear end.
1. if(front = = NULL and rear = = NULL)
a) print(Q is empty)
2. else if(front = = rear)
a) print(Q has only one item)
3. else
a) print(element at front end is front.data)
b) print(element at rear end is rear.data)
4. end if
End Queue_Status_LL

1. Circular Queues
Physically a circular array is same as ordinary array a[i-N], but logically itimplements
that a[0] comes after a[N] or a[N] comes before a[0].

The following figure shows the physical and logical representation for circular
array
 Here both Front and Rear pointers are move in clockwise direction. This is controlled
by the MOD operation.
 For e.g. if the current pointer is at i, then shift next location will
be (i + 1 % LENTH) , 1<= i <= Length
Circular Queue empty condition is
Front = -1 and Rear = -1

Circular Queue is full


Front = = (Rear + 1 % N) OR
Front == 0 and Rear == N -1
Algorithm CQ_Enqueue(item)
Input: item is new item insert in to Circular queue at rear end.
Output: Insertion of new item Circular queue at rear end if cqueue is not full.
1. if( front == ( rear + 1 % N) or (front == 0 and rear == N -1))
a) print(Circular queue is full, not possible for enqueue operation)
2. else

i) if(front = = -1 and rear = = -1) /* CQ is Empty */


a) rear=( rear + 1 % N)
b) CQ[rear]=item
c) front=0
ii) else
a) rear=( rear + 1 % N)
b) CQ[rear]=item
iii) end if
4.end if
End CQ_Enqueue

Algorithm CQ_Dequeue( )
Input: Circular Queue with some elements.
Output: Element is deleted from circular queue at front end if circular queue is not empty.
1. if(front = = -1 and rear = = -1)
a) print(CQ is empty, not possible for cqueue operation)
2. else
i) if(front = = rear) /* Q has only one element */
a) item=CQ[front]
b) front= -1
c) rear= -1
ii) else
a)item=CQ[front]
b)front=(front + 1 % N)
iii) end if
iv) print(deleted item is item)
3. end if
End CQ_Dequeue

**************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy