0% found this document useful (0 votes)
77 views15 pages

Queues: - Definition of A Queue - Examples of Queues - Design of A Queue - Deques

A queue is a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Common examples include waiting lines and mailboxes. Operations include enqueue, dequeue, peek front/rear, and check for empty/full. A circular queue solves the false overflow issue by allowing the front and rear to wrap around the queue array. Deques allow insertion and removal from both ends.

Uploaded by

Priyanshu Dimri
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)
77 views15 pages

Queues: - Definition of A Queue - Examples of Queues - Design of A Queue - Deques

A queue is a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Common examples include waiting lines and mailboxes. Operations include enqueue, dequeue, peek front/rear, and check for empty/full. A circular queue solves the false overflow issue by allowing the front and rear to wrap around the queue array. Deques allow insertion and removal from both ends.

Uploaded by

Priyanshu Dimri
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/ 15

Queues

• Definition of a Queue
• Examples of Queues
• Design of a Queue
• Deques
Definition of a Queue
• A queue is a data structure that models/enforces the
first-come first-serve order, or equivalently the
first-in first-out (FIFO) order.
• That is, the element that is inserted first into the
queue will be the element that will deleted first, and
the element that is inserted last is deleted last.
• A waiting line is a good real-life example of a
queue.
A Graphic Model of a Queue

Front:
Rear:
All items are
All new items
deleted from
are added on
this end
this end
Operations on Queues
• Insert (item): (enqueue)
– It adds a new item to the rear of the queue
• Remove ( ): (delete or dequeue)
– It deletes the front item of the queue, and returns to the caller. If
the queue is already empty, this operation returns NULL
• getFront( ):
– Returns the value in the head element of the queue
• getRear( ):
– Returns the value in the tail element of the queue
• QEmpty( )
– Returns true if the queue has no items
• Qfull()
– Returns true if Queue is full.
• size( )
– Returns the number of items in the queue
Examples of Queues
• An electronic mailbox is a queue
– The ordering is chronological (by arrival
time).
• A waiting line in a store, at a service
counter, on a one-lane road.
• Equal-priority processes waiting to run on
a processor in a computer system.
• And many more.
How Front and Rear Change
• Front increases by 1 after each dequeue( )
• Rear increases by 1 after each enqueue( )

Rear Front
Now:
49 48 47 4 3 2 1 0
Rear Front
After enqueue:
49 48 47 4 3 2 1 0
Rear Front
After dequeue:
49 48 47 4 3 2 1 0
False-Overflow Issue
• Suppose 50 calls to enqueue have been made, so now the
queue array is full

49 48 47 4 3 2 1 0
Assume 4 calls to dequeue( ) are made

49 48 47 4 3 2 1 0
• Assume a call to enqueue( ) is made now. The Rear part
seems to have no space, but the front has 4 unused spaces; if
never used, they are wasted.
Solution: A Circular Queue
• Allow the Front (and the Rear) to be moving targets
• When the Rear end fills up and front part of the array has
empty slots, new insertions should go into the front end

Rear Front

49 48 47 4 3 2 1 0
• Next insertion goes into slot 0, and Rear tracks it. The
insertion after that goes into a lot 1, etc.
Illustration of Circular Queues
• Current state:
Front

49 48 47 4 3 2 1 0
Rear
• After One Call to enqueue()
Front Rear

49 48 47 4 3 2 1 0

• After One Call to enqueue() Front Rear

49 48 47 4 3 2 1 0
Numerics for Circular Queues
• Front increases by (1 modulo capacity) after
each dequeue( ):
Front = (Front +1) % capacity;
• Rear increases by (1 modulo capacity) after each
enqueue( ):
Rear = (Rear +1) % capacity;
• Special Conditions
– Queue Empty
– Queue Full
Design of Queue
• void enqueue( int a[], int x)
{
if(front==-1 && rear == -1)
{ front=0; rear = 0; a[rear]=x; return;}
if(front%SIZE ==(rear+1)%SIZE)
{ printf(“The Queue is full”); return;}
rear =(rear+1)%SIZE;
a[rear]=x;
}
Design of Queue
• int dequeue(int a[])
{
int x;
if(rear%SIZE==(front+1)%SIZE )
{x=a[front];front=-1; rear =-1; return x;}
if (front== -1)
{printf(“Empty Queue”); return;}
else {x=a[front]; front=(front+1)%SIZE; return x;}
}
Design of Queue
• void display(int a[])
{
int i;
if(rear!=-1 && front!=-1)
{
printf(“\n\n\nThe Contents of Queue is \n”);
for( i=front;i<(rear-1)%SIZE, i=(i+1)%SIZE)
printf(“%d\t”, a[i]);
}
}
Design of Queue
#include<stdio.h>
int front = -1; rear = -1, SIZE=10, a[10]={0};
void main(int a[])
{
int p;
enqueue(a, 12);
enqueue(a, 20);
enqueue(a, 30)
display(a);
p=dequeue(a);
printf(“\nThe element deleted from queue is %d”, p);
display(a);
}
Deques
• A deque is a double-ended queue—items can be
inserted and removed at either end
• Implementation is similar to that for queues
• Two versions – Input restricted and Output restricted

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