PREPARED BY:Mitali sonar (lect. Computer...

Preview:

Citation preview

PREPARED BY:Mitali sonar

(lect. Computer Dep.)

A queue is a linear list in which elements can be

added at one end and elements can be removed

only at other end.

So the information in this list is processed in same

order as it was received means

FIRST IN FIRST OUT(FIFO)

Ex:-people waiting in a line

at a bus stop.

The first person in queue is

the first person to take bus

. Whenever new person

comes he joins at end of the

queue.

The end of the queue from which the element is

deleted is known as front and the end from which

elements are inserted is known as rear.

INSERTION is made to right most(REAR) element is

called ENQUEUE

DELETION is made to left most element(FRONT)

is called DEQUEUE

FRONT REAR

INSERTIONDELETION

0 1 2 3 4 … … MAX-1

F = -1, R= -1An Empty queue

9

0 1 2 3 4 … … MAX-1

F = 0,R= 0Queue after inserting one element

9 5 3

0 1 2 3 4 … … MAX-1

F = 0

Queue after inserting few element

R =2

5 3

0 1 2 3 4 … … MAX-1

F = 1 Queue after deleting one elementR =2

3

0 1 2 3 4 … … MAX-1

Queue after deleting second element

F=2,R =2

3 8 2 7

0 1 2 3 4 … … MAX-1

Queue having vacant space

F=2 R=MAX - 1

1)using an ARRAY :-

A one dimensional array Q[0,1,2…………..N] can

be used to represent a queue.

Since the array is a static data structure

representation requires maximum size of the

queue to be predetermined and fixed.

Queue keeps changing as elements are

inserted and deleted the maximum size should

be large enough to expand or shrink.

Two pointers FRONT and REAR indicates two

end of the queue.

…………………… …………………….

F R

0 1 2 NN-1

Three states of a queue

1 ) Queue is empty

FRONT =-1

REAR =-1

2) Queue is full

FRONT = 0

REAR = N

3) Queue contains element >=1

FRONT <= REAR

Using an linked list

Sometime length of the queue cannot be

predicated before.

To overcome this problem use link list

To represent the queue we are using double linked

list.

Pointers FRONT & REAR points to the 1st node and

last node in to list.

DATA .

DATA

DATA . -------

-------

HEADER FRONT

REAR

Two state of the queue

Queue is empty

FRONT = REAR = HEADER

HEADER RLINK = NULL

Queue contains at least 1 element

HEADER RLINK != NULL

Queue is a …………………………

A) linear data structure

B) non linear data structure

C) both (a) & (b)

D) none of the above

The end of the queue from which element is

deleted is called………….. & The end from

which element is inserted is called………..

For queue implemented as array the initial

value of front & rear is set to ………………

Q :- Queue with n element

F :- front pointer

R :- Rear pointer

Y: -element to be inserted

1 [overflow?]

if R>= N

then write (“overflow”)

2 [Increment rear pointer]

R R + 1

3 [Insert an element]

Q[R] y

4 [Is front pointer properly set?]

if F=0 then f1

Return

Q :- Queue with n element ,F :- front pointer

R :- Rear pointer

1 [Underflow]

if F = -1

then write(“underflow”)

return

2 [Delete element]

y Q[F]

3 [Queue is empty]

if F=R

then F R -1

else F F + 1

4) [Return to element]

Return (y)

Draw a queue using following data.

Consider a size 6. Assume that the queue is

initially empty. It is required to insert

element 1,2 & 3 followed by delete 1 & 2 &

insert 4,5 & 6.

c d

Front

Rear

Front

e c d

Rear

Solution : Recycle / Wrap Around

In a linear queue when REAR pointer reaches

the end, insertion will be denied. even if

memory block is available at front.

One way to avoid this is to use circular queue

In Circular queue elements are Q[0,1,2……..,N]

but arranged in circular fashion with Q[0] following Q[N].

n n-1 i 1

FRONTREAR

LOGICAL VIEW

PHYSICAL VIEW

the most common use of

a circular queue is

in operating system

& in Real Time

Application’s programs

Advantages of circular queue over simple

queue

In circular queue we can insert new item to the

location from where previous item to be deleted

In circular queue we can insert n numbers of

elements continuously but condition is that we

must used deletion. Where as in simple queue

continuously insertion is not possible.

1) [Reset rear pointer]

if R= N then R1

else R R + 1

2) [overflow]

if F =R then write(“overflow”)

return

3) [insert element]

Q[R] y

4) [Is front pointer properly set]

If F= -1 then F 0

return

1 [Underflow]

if F = -1

then write(“underflow”)

return

2 [Delete element]

y Q[F]

3 [Queue is empty]

if F=R then F R -1

return(y)

4) [Increment front pointer]

if F =N then F1

else F F + 1

It’s a linear list in which element can be

inserted or deleted at either end of structure

That is elements can be inserted/deleted

to/from the rear or the front end.

A dequeue can be used as stack well as queue

There are 2 variations of dequeue

1) INPUT RESTRICATED QUEUE

2) OUTPUT RESTRICATED QUEUE

FRONT REAR

DELETION

INSERTION

INSERTION

DELETION

1) INPUT RESTRICATED QUEUE

It allows insertion only at one end

2) OUTPUT RESTRICATED QUEUE

It allows deletion from one end only

FRONT REAR

DELETION

INSERTIONINSERTION

FRONT REAR

DELETION

INSERTIONDELETION

In priority queue each element is assigned a

value called priority & elements are inserted

or deleted according to that priority

So an element can be inserted or deleted not

only at end but at any position on the queue

It does not strictly follows FIFO order

A B …… …… X ….. …… P

P1 P1 Pi PN

FRONT REAR

In implementing a priority queue 2 rules are

applied

1. The element with higher priority processed

before any element of lower priority

2. The element with same priority are processed

according to the order in which they were

added to the queue.

There are various ways of implementing

priority queue

1. Using simple / circular array

2. Multi queue implementation

3. Using double link list

4. Using heap tree

Fig. shows priority queue of jobs waiting to

use a computer

Fig B shows how single priority queue can be

visualized as three separate queue

Elements in second queue are removed only

when first queue is empty& elements from

third queue is removed only when first &

second queue are empty

R1 R2 ……. Ri-1 O1 O2 ……. Oj-1 B0 B1 ……. Bk-1

1 1 1 2 2 2 3 3 3

Ri Oj Bk

R1 R2 ………... Ri-1

1 1 1

Ri

O1 O2 ……….. Oj-1

2 2 2Oj

B0 B1 ……….. Bk-1

3 3 3

Bk

Fig B

Priority 1

Priority 2

Priority 3

For each priority a queue is maintained & for

each queue two pointers front & rear pointers

are maintained.

The element with given priority number is

inserted in the corresponding queue.

SIMULATION

It’s a process of forming an abstract model from

a real situation in order to understand the input

of modification and effect of introducing various

strategies on situation.

It’s a process of modeling real life situation

through a computer program

Its main use is to study a real life situation

without actually making it to occur.

Mainly used in areas like military, scientific

research where it is expensive or dangerous to

experiment with real system.

Any process that is to be simulated is called

SYSTEM.

Ex:-Ticket reservation system having 4 counter. If

a customer arrives at a time ta & counter 1 is

free then customer will get ticket immediately.

It is not possible that always counter is free.In

that case customer goes to queue having less

number of customers.

Assume time required is t then total time spent

by a customer equals time t + time spent in

waiting in line.

The avg time spent by customer can be computed

by a program simulating customer action.

This program can be implemented using queue

since while one customer is being served other

keep on waiting.

Multiprogramming means multiple programs runs

concurrently to increase cpu utilization. All

processes that are residing in memory & are ready

to execute are kept in READY QUEUE.

Process are divided in 3 groups system process

,interactive process, batch process. To each group

priority is assigned.

SYSTEM PROCESSHigh priority

BATCH PROCESSLow priority

INTERACTIVE PROCESSMedium priority

The high priority queue processed gets executed

before lower priority process.

Each queue is assigned certain interval of time.

They are also allowed to move between the

queue. If a process uses too much CPU time

then it is moved to lower priority

Similarly if a process that is waiting for too long

in lower priority queue is moved to higher

priority queue.

Recommended