Data Structures & Algorithms Data Structures & Algorithms

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Data

Data Structures
Structures &
& Algorithms
Algorithms

Queue & its basic operations


Objectives
Objectives
• Today’s lecture objectives include:
– Introduction to Queues

– Queue Applications

Data Structures & Algorithms 2


Queues
Queues -- Introduction
Introduction
• In English--- Waiting line
– Line of people to purchase tickets

• Linear data structure

• Follows a particular order in which the operations


are performed
– FIFO principle – first in first out
• Difference from Stack:
– Insertion go at the end of the list, rather than the
beginning of the list
Data Structures & Algorithms 3
Queue
Queue
• A Queue is a special kind of list,
– where items are inserted at one end (the rear) and
– deleted at the other end (the front)
• Stores a set of elements in a particular order
– Also known as FIRST-IN FIRST-OUT (FIFO)
– OR LAST-IN LAST-OUT
• Example
– The first one in line is the first one to be served

Data Structures & Algorithms 4


Queue
Queue
• Rear (R):
– Insertion Pointer
• Front (F):
– Deletion Pointer

D rear D rear
C rear C C
B rear B B B front
rear front A
A A front A front
front

Data Structures & Algorithms 5


Queue
Queue Applications
Applications
• Real life examples
– Waiting in line
– Waiting on hold for tech support

• Applications related to Computer Science


– Threads
– Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

• Operating system
– multi-user / multitasking environments,
• where several users / task may be requesting the same resource
simultaneously.

Data Structures & Algorithms 6


Queue
Queue Applications
Applications –– Cont…
Cont…
• Job Scheduling

Front Rear Q[0] Q[1] Q[2] Q[3] Comments


-1 -1 queue is empty

-1 0 J1 Job 1 is added

-1 1 J1 J2 Job 2 is added

-1 2 J1 J2 J3 Job 3 is added

0 2 J2 J3 Job 1 is deleted

1 2 J3 Job 2 is deleted

Data Structures & Algorithms 7


Queue
Queue Common
Common Operations
Operations
1. MAKENULL(Q): Makes Queue Q be an empty list.
2. FRONT(Q): Returns the first element on Queue Q.
3. ENQUEUE(x, Q): Inserts element x at the end of
Queue Q.
4. DEQUEUE(Q): Deletes the first element of Q.
5. EMPTY(Q): Returns true if and only if Q is an empty
queue.

Data Structures & Algorithms 8


Example:
Example: Enqueue
Enqueue and
and Dequeue
Dequeue

Data Structures & Algorithms 9


Queue
Queue Implementation
Implementation
• Static
– Queue is implemented by an array, and size of queue remains fix

• Dynamic
– A queue can be implemented as a linked list, and
– Can expand or shrink with each enqueue or dequeue operation.

Data Structures & Algorithms 10


Static
Static Queue
Queue Implementation
Implementation

Data Structures & Algorithms 11


Static
Static Queue
Queue Implementation
Implementation

Data Structures & Algorithms 12


Dynamic
Dynamic Queue
Queue
• ENQUEUE(y,Q)

• DEQUEUE(Q)

Data Structures & Algorithms 13


Queue
Queue –– enqueue
enqueue
void enqueue(int rear, int item)
{
// add an item to the queue
if (rear == MAX_QUEUE_SIZE)
{
queue_full( );
return;
}
rear = rear + 1;
queue [rear] = item;
}

Data Structures & Algorithms 14


THANK
THANK YOU
YOU

Data Structures & Algorithms 15

You might also like