Queue

You might also like

You are on page 1of 57

QUEUE

QUEUE

 A queue is an ordered collection of items from which items


may be deleted at one and into which items may be inserted at
the other end.
QUEUE

 Figure illustrates a queue containing three elements. A, B


and C, A is at the front of the queue and C is at the rear.

FRONT

A B C

REAR
QUEUE

 Figure illustrates an element has been deleted from the queue.


since elements may be deleted only from the front of the queue, A
is removed and B is now at the front.

FRONT

B C

REAR
QUEUE

 When items D and E are inserted they must be


inserted at the rear of the queue.

FRONT

B C D E

REAR
FIFO

 The first element inserted into a queue is the first


element to be removed. For this reason a queue is
sometimes called a FIFO list as opposed to a stack,
which is LIFO.
QUEUE

 Placing an item in a queue is called “insertion


or enqueue”, which is done at the end of the
queue called “rear”.
 Removing an item from a queue is called
“deletion or dequeue”, which is done at the
other end of the queue called “front”.
 Some of the applications are : printer queue,
keystroke queue, etc.
QUEUE

 Initially q.rear is set to -1, and q.front is set to 0.


 The queue is empty whenever q.rear<q.front.

 The number of elements at any time is equal to the value of


q.rear-q.front+1
ADT-QUEUE

 Queue_full(struct queue q);


 Precondition:
 The queue has been created.
 Postcondition:
 The function returns 1 if q.rear==max-1 otherwise it
returns 0.
ADT-QUEUE

 Enqueue(struct queue q, dataitem x);


 Precondition:
 The queue has been created and is not full.
 Postcondition:
 The entry x has been stored in the queue as its last
element.
ADT-QUEUE

 Queue_empty(struct queue q);


 Precondtion :

Queue has been created;


 Postcondition:

Returns 1 if queue q.rear<q.front, otherwise it returns


0.
ADT-QUEUE

 X=Dequeue(struct queue q);


 Precondition:
 The queue has been created and is not empty.
 Postcondition:
 The first element in queue has been removed and returned
as the value of X.
IMPLEMENTATION

#define size 5
struct queue
{
int front,rear;
int a[size];
};
IMPLEMENTATION-ENQUEUE
void Enqueue(struct queue *q,int x)
{
if(q->rear==size-1)
{
printf(“Queue is full\n");
}
else
{
//q->rear=q->rear+1;
q->a[++q->rear]=x;
}
}
IMPLEMENTATION-DEQUEUE

int Dequeue(struct queue *q)


{ int x;
if(q->rear<q->f ront)
{ printf("queue empty\n"); return 0;
}
else
{
//x=q->a[q->f];
return q->a[q->f++];
//return x;
}
}
IMPLEMENTATION
void main()
{
struct queue q1; int n,ch,x;
q1.f=0;q1.r=-1;
do{
scanf("%d",&n);
Enqueue(&q1,n);
printf("press 1 if u wanna add more elements");
scanf("%d",&ch);
}while(ch==1);
do{
x=dequeue(&q1);
printf("\n\n deleted element=%d",x);
printf("press 1 if u wanna delete %d");scanf("%d",&ch);
}while(ch==1);
}
EXAMPLE
q.items

2
1
0 q.front=0
q.rear=-1
EXAMPLE
q.items

2
1
0 10 q.front=0
q.rear=0
EXAMPLE
q.items

2
1 20 q.rear=1
0 10 q.front=0
EXAMPLE

q.items

2 30 q.rear=2
1 20
0 10 q.front=0
EXAMPLE

q.items

2 30 q.rear=2
1 20 q.front=1
0
EXAMPLE

q.items

2 30 q.rear=q.front=2
1
0
Example

 To insert(q,60) ?

q.items

4 50 q.rear=4
3 40

2 30 q.front=2
1
0
SOLUTIONS

 Solution 1:
 Modify the remove operation so that when an item is
deleted , the entire queue is shifted to the beginning of the
array.
x=q.items[0];
for(i=0;i<q.rear;i++)
q.items[i]=q.items[i+1];
q.rear--;
 Disadvantage?
 Solution 2?
CIRCULAR QUEUE

 Solution 2:
 To view the array that holds the queue as a circle rather
than as a straight line. That is , we imagine the first
element of the array (element at position 0) as immediately
following its last element
 Initialization
 Front=-1
 Rear=-1
INSERT OPERATION

 Step1:
if(Front==(Rear+1)%maxsize)
queue overflow and goto step 2
else
if(Front==-1)
set Front=Rear=0
else
Rear=(Rear+1)%maxsize
Queue[rear]=item
 Step 2: Exit
DELETE OPERATION

 Step1:
if(Front==-1)
Queue underflow and goto step2
else
item=queue[Front]
if(Front==Rear)
Set Front=Rear=-1
else
Front=(Front+1)%maxsize
 Step2: Exit
 Front=-1
 Rear=-1

 Maxsize=4
 Qinsert(q1,10)
 Qinsert(q1,10)
 Front=0,Rear=0

 Q[Rear]=10

3 0 Front
10 Rear

2 1
 Qinsert(q1,20)
 Qinsert(q1,20)
 Front=0,Rear=1

 Q[Rear]=20

3 0 Front
10

20 1
2
Rear
 Qinsert(q1,30)
 Qinsert(q1,30)
 Front=0,Rear=2

 Q[Rear]=30

3 0 Front
10

30
20 1
2

Rear
 Qinsert(q1,40)
 Qinsert(q1,40)
 Front=0,Rear=3

 Q[Rear]=40

Rear 3 0 Front
10
40

30
20 1
2
 Qinsert(q1,50)
 Qinsert(q1,50)
 Front=0,Rear=3

 Queue full

Rear 3 0 Front
10
40

30
20 1
2
 Qdelete(q1)
 Item=10

 Front=1,Rear=3

Rear 3 0
40

30
20 1
2
Front
 Qdelete(q1)
 Item=20

 Front=2,Rear=3

Rear 3 0
40

30
2 1

Front
 Qdelete(q1)
 Item=30

 Front=3,Rear=3

3 0
Rear
Front 40

2 1
 Qinsert(q1,50)
 Front=3,Rear=0

 Q[Rear]=10

Front 3 0 Rear
50
40

2 1
 Qdelete(q1)
 Item=40

 Front=0,Rear=0

3 0 Front
50 Rear

2 1
 Qdelete(q1)
 Item=50

 Front=-1,Rear=-1

3 0

2 1
PROBLEM
DE QUEUE

 Double ended queue


 Insertion and deletion done at both the ends
 Operations
 Insert_front

 Insert_rear

 Delete_front

 Delete_rear
TYPES : DE-QUEUE

 Input restricted deque:


 Output restricted deque:
INPUT RESTRICTED DEQUE
 In the input restricted deque deletion operations are
performed at both ends whereas only insertion operation
is performed at only one end.

FRONT

Insertion
Deletion B C D E Deletion

REAR
OUTPUT RESTRICTED DEQUE
 In this insertion is done at both the ends but deletion is
restricted at one end.

FRONT

Insertion Insertion
Deletion B C D E

REAR
PRIORITY QUEUE

 Priority queue is a data structure in which the


intrinsic ordering of elements does determine the
results of its basic operations.
 Ascending priority queue
 Descending priority queue
ASCENDING PRIORITY QUEUE

 Ascending priority queue is a collection of items into


which items can be inserted arbitrarily and from
which only the smallest item can be removed.
DESCENDING PRIORITY QUEUE

 Descending priority queue is a collection of items into


which items can be inserted arbitrarily and from
which only the largest item can be removed.
APPLICATION OF PRIORITY QUEUE

 Generally the priority queue is applicable when the


process is allocated with the help of operating system.
i.e., in the process scheduling, the priority queue is
used.
 Implementation?
 The scratchemup Parking garage contains a single lane that holds up
to ten cars. Cars arrive ate the south end of the garage and leave
from the north end. If a customer arrives to pick up a car that is not
the northenmost, all cars to the north of the car are moved out., the
car is driven out, and the other cars are restored in the same order
that they were in originally. Wherever a car leaves, all cars to the
south are moved forward so that at all times all the empty spaces are
in the south of the garage. Write a program that reads a group of
lines each line contains an ‘A’ for arrival or ‘D’ for departure and a
license plate number. Cars are assumed to arrive and depart in the
order specified by the input. The program should print a message
each time that a car arrives or departs. When a car arrives, the
message should specify whether or not there is room for the car in
the garage. If there is no room for a car, the car waits until there is
room or until a departure line is read for the car. When room
becomes available, another message should be printed. When a car
departs, the message should include the number of times the car was
moved within the garage, including the departure itself but not eh
arrival. This number is 0 if the car departs from the waiting line.
APPLICATION OF QUEUES

 Simulation
 Simulation is the process of forming an abstract model from a real
situation in order to understand the behaviour of the system under
simulation.
 The computers are used to initiate the behaviour of a real system
and the parameters governing the system can be modified
conveniently and studied. After running the simulation for a
predefined time, the results would give the scientist or system
analyst for any changes to be incorporated in the system.
THE ADVANTAGE OF CONDUCTING SIMULATION A PARTICULAR
SYSTEM

 Some system may be too expensive


 Some system may be too dangerous

 Some system may be too time consuming.


 Examples of simulated systems requiring queues are:
 Billing center in a departmental store (customer standing
in a queue)
 Airport (landing and take off assuming only one runway)
 Ships waiting in a queue in shore
 Processes stored in a ready queue waiting for the processor
time in a computer system.
 Jobs waiting in a queue for the printer(spooling)
 Single queue single server systems (railway, Airport, Bus
station etc., reservation counters)

You might also like