0% found this document useful (0 votes)
190 views30 pages

Unit 2 - QUEUE

The document discusses queues as a linear data structure that follows a First-In First-Out (FIFO) approach, with new items added to the rear of the queue and existing items removed from the front. It covers queue operations like enqueue, dequeue, peek, and examples of queue usage in real-world scenarios like bus lines. Common queue types include simple, circular, priority, and double-ended queues.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views30 pages

Unit 2 - QUEUE

The document discusses queues as a linear data structure that follows a First-In First-Out (FIFO) approach, with new items added to the rear of the queue and existing items removed from the front. It covers queue operations like enqueue, dequeue, peek, and examples of queue usage in real-world scenarios like bus lines. Common queue types include simple, circular, priority, and double-ended queues.

Uploaded by

Lavanya J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT II LINEAR DATA

STRUCTURES – STACKS AND


QUEUES

QUEUE
Introduction
• It is a non primitive data structure, designed with
restricted random access.
• This also stores data in continuous memory locations.
• Even though it seems like arrays and stacks, it has its
own uniqueness in organizing and accessing data.
• This is the most common data structure that can be
seen in our day to day life.
• Queue is an abstract data structure, somewhat similar
to stack.
• In contrast to stack, queue is opened at both end.
• One end is always used to insert data enqueue and
the other is used to remove data dequeue.
• Queue follows First-In-First-Out methodology, i.e.,
the data item stored first will be accessed first.
• A queue is a data structure that a linear collection of items in which
access is restricted to a first-in first-out basis.
• New items are inserted at the back and existing items are removed
from the front.
• The items are maintained in the order in which they are added to the
structure.
• A new element is added at one end
called rear end and the existing
elements are deleted from the other
end called front end.
• Enqueue is done at the front of the
queue and dequeue is done at the end 
of the queue.
• Total no of elements
in queue= rear-front+1
• Queue operations may involve initializing or defining the queue,
utilizing it and then completing erasing it from memory.
• Here we shall try to understand basic operations associated with
queues
enqueue − add store an item to the queue.
dequeue − remove access an item from the queue.
• Few more functions are required to make above mentioned queue
operation efficient. These are
peek − get the element at front of the queue without removing
it.
isfull − checks if queue is full.
isempty − checks if queue is empty.
• In queue, we always dequeue or access data, pointed by front
pointer and while enqueing or storing data in queue we take help
of rear pointer.
• The practical examples of queues are
– The consumer who comes first to a shop will be
served first. 
– CPU task scheduling and disk scheduling.
– Waiting list of tickets in case of bus and train
tickets.
Usage Scenario

FRONT REAR
BUS STOP
Usage Scenario

FRONT REAR
BUS STOP
Usage Scenario

FRONT REAR
BUS STOP
Usage Scenario

FRONT REAR
BUS STOP
Types of Queue
Queue in data structure is of the following types
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Deque (Double Ended Queue)
1. Single ended Queue (Simple)
• A simple queue is the most basic queue. In this queue, the
enqueue operation takes place at the rear, while the
dequeue operation takes place at the front:

• Its applications are process scheduling, disk scheduling,


memory management, IO buffer, pipes, call center phone
systems, and interrupt handling.
2. Circular Queue
• A circular queue permits better memory utilization than a simple
queue when the queue has a fixed size.
• In this queue, the last node points to the first node and creates a circular
connection. Thus, it allows us to insert an item at the first node of the queue
when the last node is full and the first node is free.
• It’s also called a ring buffer

• It’s used to switch on and off the lights of the traffic signal systems. Apart
from that, it can be also used in place of a simple queue in all the
applications mentioned above.
3. Priority Queue
• A priority queue is a special kind of queue in which each item has a
predefined priority of service. In this queue, the enqueue operation takes
place at the rear in the order of arrival of the items, while the dequeue
operation takes place at the front based on the priority of the items.
• That is to say that an item with a high priority will be dequeued before an
item with a low priority.
• In the case, when two or more items have the same priority, then they’ll be
dequeued in the order of their arrival. Hence, it may or may not strictly
follow the FIFO rule:

• It’s used in interrupt handling, Prim’s algorithm, Dijkstra’s


algorithm,  heap sort, and Huffman code generation.
4. Double-Ended Queue (Deque)
• A deque is also a special type of queue. In this queue, the enqueue and
dequeue operations take place at both front and rear.
• That means, we can insert an item at both the ends and can remove an item
from both the ends. Thus, it may or may not adhere to the FIFO order:

• It’s used to save browsing history, perform undo operations, implement A-


Steal job scheduling algorithm, or implement a stack or implement a
simple queue.
Operations on Queue
1. Peek/Front: Like stacks, this function helps to see
the data at the front of the queue.
Algorithm of peek/front function
Operations on Queue
2. isfull: As we are using single dimension array to
implement queue, we just check for the rear pointer
to reach at MAXSIZE to determine that queue is
full.
Algorithm of isfull function
Operations on Queue
3. isempty: If value of front is less than MIN or 0, it tells
that queue is not yet initialized, hence empty.
Algorithm of isempty function
Operations on Queue
4. Enqueue:As queue maintains two data pointers, front
and rear, its operations are comparatively more
difficult to implement than stack.
The following steps should be taken to enqueue insert
data into a queue −
Step 1 − Check if queue is full.
Step 2 − If queue is full, produce overflow error and exit.
Step 3 − If queue is not full, increment rear pointer to point next
empty space.
Step 4 − Add data element to the queue location, where rear is
pointing.
Step 5 − return success.
Step 1 :If REAR = (MAXSIZE –1) : then //QUEUE FULL
Write : “Queue Overflow” and return
[End of If structure]
Step 2 : Read NUM to be inserted in Linear Queue.
Step 3 : Set REAR := REAR + 1
Step 4 : Set QUEUE[REAR] := NUM
Step 5 : If FRONT = –1 : then
Set FRONT=0.
[End of If structure]
Step 6 : Exit
Operations on Queue
Algorithm for Enqueue operation
Operations on Queue
5. Dequeue:Accessing data from queue is a process of
two tasks − access the data where front is pointing
and remove the data after access
The following steps should be taken to dequeue insert
data into a queue −
Step 1 − Check if queue is empty.
Step 2 − If queue is empty, produce underflow error and exit.
Step 3 − If queue is not empty, access data where front is
pointing.
Step 4 − Increment front pointer to point next available data
element.
Step 5 − return success.
Step 1 : If FRONT = -1 : then // QUEUE EMPTY
Write : “Queue Underflow” and return
[End of If structure]
Step 2 : Set NUM := QUEUE[FRONT]
Step 3 : Write “Deleted item is : ”, NUM
Step 4 : Set FRONT := FRONT + 1.
Step 5 : If FRONT>REAR : then
Set FRONT := REAR := -1.
[End of If structure]
Step 6 : Exit
Operations on Queue
Algorithm for dequeue operation
3 States of Queue
• Queue is empty
FRONT = REAR
• Queue is Full
REAR=N
(REAR+1) mod n == FRONT
• Queue contains elements >=1
FRONT< REAR
NO. OF ELEMENT= REAR-FRONT+1

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