0% found this document useful (0 votes)
14 views17 pages

Data Structure3

Uploaded by

rbousoboh3000
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)
14 views17 pages

Data Structure3

Uploaded by

rbousoboh3000
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/ 17

Algorithm and Data Structures1

Abstract Data Structures (ADS)


Queue

Dr. Leena murad Lecture 6


Queue

 A queue is a data structure for storing a number of data items that have the same
type.
 It can be accessed at two ends.
 Items are inserted at the one end ( called the rear end or tail of queue).
 items may be deleted at other end (called front or head of the queue).
 A queue obeys the First In First Out (FIFO) principle, which is identical to the Last
In Last Out (LILO) principle.
Dequeue Enqueue
Operations of a Queue

 Two main operations are applied to a queue:

Enqueue: adding an item to the end of the queue.


Dequeue: removing the first item of the queue.
 In addition to :

Clear: (also called flush or reset) removes all the items of the queue (destroy or flush the queue).
IsEmpty: A boolean function that returns true if the queue is empty.
IsFull: A boolean function that returns true if the queue is full.
First: A function that returns the value of the first item without dequeuing it.
Applications of Queue

 The printer spooler

 Traversing a tree or graph in a breadth first manner


 A requests for access to a file-server are arranged on a queue, so the first request
comes to the server served first.
 Calls to a large companies are generally placed on a queue when all operators are
busy.
 Simulating real life queuing situations (customer service, aircraft takeoff and landing
queues in an airport, etc ).
Queue Implementation

 Two ways:
Using an array (ordinary or dynamic).
Using a linked list (singly or doubly linked).
Implementations using dynamic array

void Enqueue(int data)


struct Queue { {
int front, rear, capacity; if (rear == capacity-1)
int *queue; cout<<"\nQueue is full\n";
Queue(int c) else if(front ==-1)
{front = rear = -1; {
capacity = c; front++;
queue = new int[c]; rear++;
} queue[rear] = data;
~Queue() { delete[] queue; } }
else
{
rear++;
queue[rear] = data;

}
}
Implementations using dynamic array
// print queue elements
void Display()
void Dequeue() {
{ int i;
if (front == -1) if (front == -1)
cout<<"\nQueue is empty\n"; cout<<"\nQueue is Empty\n";
else { // traverse front to rear and print elements
for (int i = 0; i < rear; i++) for (i = 0; i <= rear; i++)
queue[i] = queue[i + 1]; cout<<queue[i]<<"---";
rear--; }
if (rear==-1)front =-1; // print front of queue
} void queueFront()
} {
if (front == -1)
cout<<"\nQueue is Empty\n";
else
cout<<"\nFront Element is:"<< queue[front];
}};
Implementations using dynamic array

void main(void)
{// Create a queue of capacity 4
Queue q(4);
q.Display();
q.Enqueue(20);
q.Enqueue(30);
q.Enqueue(40);
q.Enqueue(50);
q.Display();
q.Enqueue(60);
q.Display();
q.Dequeue();
q.Dequeue();
cout<<"\n\nafter two node deletion\n\n";
q.Display();
q.queueFront();
}
Implementations using linked list

struct node { void Dequeue() int main()


int data; { {
node* next; if (front == NULL) Enqueue(10);
}; cout<<"\nempty\n"; Enqueue(20);
node *front=0, *rear=0; else Dequeue();
void Enqueue(int x) { Dequeue();
{ node* temp = front; Enqueue(30);
node* temp = new node; front = front->next; Enqueue(40);
if (rear == NULL) if (front == NULL) Enqueue(50);
front = rear = temp; rear = NULL; Dequeue();
else{ delete (temp); cout << "Queue Front : "
temp->data=x; } << front->data << endl;
temp->next=0; } cout << "Queue Rear : "
rear->next = temp; << rear->data;
rear = temp; }
} }
Time Complexity

Enqueue Dequeue

array O(1) O(n)

Linked list O(1) O(1)


Types of Queues

There are four different types of queues:


 Simple Queue
 Circular Queue
 Priority Queue
 Double Ended Queue
Circular Queue

 In a circular queue, the last element points to the first element making a circular link.
 The main advantage of a circular queue over a simple queue is better memory
utilization.
 If the last position is full and the first position is empty, we can insert an element in
the first position. This action is not possible in a simple queue.
Circular Queue
rear = 0 rear = 6
rear = 1 front = 0
rear = -1, front = -1 front = 0 front = 0

rear = 6 rear = 0
front = 1 front = 1
Priority Queue

 A Priority Queue is a queue in which items have a priority criterion. When dequeuing
such a queue, the first item with highest priority is chosen for dequeuing.
 If elements with the same priority occur, they are served according to their order in
the queue.
Priority Queue

 An example of a priority queue is a queue of Patients in a hospital: Generally


Patients are served on a fist-come first-served basis, but if a Patient is in critical
condition he/she is give priority over other Patients waiting before him/her in
the queue.
Deque (Double Ended Queue)

 In a double ended queue, insertion and removal of elements can be performed from
either from the front or rear. Thus, it does not follow the FIFO (First In First Out)
rule.

Enqueue Enqueue
Dequeue Dequeue
Stack VS. Queue
BASIS FOR
STACK QUEUE
COMPARISON
Working principle LIFO (Last in First out) FIFO (First in First out)
Structure Same end is used to insert and delete One end is used for insertion, i.e., rear end
.elements and another end is used for deletion of
.elements, i.e., front end
Number of pointers used One Two (In simple queue case)
Operations performed Push and Pop Enqueue and dequeue
Examination of empty Top == -1 Front == -1
condition
Examination of full Top == Max - 1 Rear == Max - 1
condition

Variants .It does not have variants It has variants like circular queue, priority
.queue, doubly ended queue

Implementation Simpler Comparatively complex

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