Professional Documents
Culture Documents
Queue
Queue
Queue
QUEUE
FRONT
A B C
REAR
QUEUE
FRONT
B C
REAR
QUEUE
FRONT
B C D E
REAR
FIFO
#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
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
Insert_rear
Delete_front
Delete_rear
TYPES : DE-QUEUE
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
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