Docsity

Queues: A Special Kind of List for Efficient Data Processing, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
Queues
Queues
Queues
“A Queue is a special kind of list, where items
are inserted at one end
(
the rea
r
)
And deleted
(
)
at the other end (the front)”
Other Name:
First In First Out (FIFO)
First
In
First
Out
(FIFO)
Common O
p
erations on
Q
ueues
pQ
(Queue ADT)
1. ISNULL(): Return true if Queue is Full.
2. FRONT(
)
:Returns the first element on
ueue.
)
3. ENQUEUE(Q): Inserts element Q at the end.
4
DEQUEUE():
Deletes the first element
4
.
DEQUEUE():
Deletes
the
first
element
.
5. EMPTY(): Returns true if and only if Queue is
t
emp
t
y.
Example:
Example:
Line of customers in a bank
Aliti fQ
A
pp
li
ca
ti
ons o
f
Q
ueues
•
Operating system
•
Operating
system
– multi-user/multitasking environments, where
several users or task may be requesting the same
several
users
or
task
may
be
requesting
the
same
resource simultaneously.
•
Comm nication Soft are
•
Comm
u
nication
Soft
w
are
– queues to hold information received over networks
ddi l ti (I f ti b
an
d
di
a
l
up connec
ti
ons.
(I
n
f
orma
ti
on can
b
e
transmitted faster than it can be processed, so is
placed in a queue waiting to be processed)
placed
in
a
queue
waiting
to
be
processed)
• Some other?
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Queues: A Special Kind of List for Efficient Data Processing and more Slides Data Structures and Algorithms in PDF only on Docsity!

Queues

QueuesQueues

“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

Other Name: ƒ^ First In First Out (FIFO)ƒ^ First In First Out (FIFO)

Common Operations on Queues

p^

Q

(Queue ADT)

1.^

ISNULL():

Return true if Queue is Full.

2.^

FRONT

( ):^

Returns the first element on Queue. )

Q

3.^

ENQUEUE(

Q ):

Inserts element Q at the end.

DEQUEUE():

Deletes the first element

4.^

DEQUEUE():

Deletes

the first element

5.^

EMPTY():

Returns true if and only if Queue is

t

empty.

Example:Example: Line of customers in a bank

A^

li^

ti^

f Q

Applications of Queues

• Operating system• Operating system

  • multi-user/multitasking environments, whereseveral users or task may be requesting the sameseveral users or task may be requesting the sameresource simultaneously.

• Comm nication Soft

are

• Communication Software

  • queues to hold

information

received over networks

d di l

ti^

(I f

ti^

b

and dial up connections. (Information can betransmitted faster than it can be processed, so isplaced in a queue waiting to be processed)placed in a queue waiting to be processed)

• Some other?

docsity.com

Bus Stop QueueBus

Stop Queue

BusSStop

front

rear

rear^

rear^

rear

rear

Bus Stop QueueBus Stop Queue

BusSStop

front

rear^

rear

rear

Bus Stop QueueBus Stop Queue

BusSStop

front

rear

rear

Bus Stop QueueBus Stop Queue

BusSStop

front

rear

rear

docsity.com

But the best Solution is:Using circular queue

• Allow rear to wrap around the array.

p^

y

if(rear == queueSize-1)

rear = 0;

else

rear++;

d l

i h

i

• Or use module arithmetic^ rear = (rear + 1) % queueSize;

Ci

l^

A

Ci

rcular Array

Advance the front to dequeue an item.

Si^

f 0

Size - 1 6

front 1 4 1

back 3 4

back

Ci

l^

A

Circular Array

T^

d^

h^

b^

k^

i^

d i^

i^ i

To enqueue, advance the back pointer, and insert in itsnew position.

Size

Size - 1 6

front 2 4 1

front 2 3 4 5

4 back

Ci

l^

A

Circular Array

I^

h^

f^

i^

l^ i

h^ f

d b

k

i

In the case of a single item, the front and back point tothe same index.

Size

Size - 1 6

front

back

docsity.com

Ci

l^

A

Ci

rcular Array

The array is empty when the front is ahead of the back.

Size

Size - 1 6

front

back

Ci

l^

A

Circular Array

H^

hi^

i^

h^

b^

h^

h^

i

-^ However, this is the same case, but when the array isfull.

Size

Size - 1 6

front

4 back

Ci

l^

A

Circular Array

Th^

f^

k^

d^

i^

h^

h

Therefore, you must keep a count to determine when thearray is empty and when it is full.

Si^

Size - 1 6

count = size; 1 4 3

(^4 )

Full !! 2

front

4 back

7

6

12

67

7

6

12

67

0

1

2

3

4

5

6

7

8

Front=5Front=5 Rear= Enqueue 39

Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0

39

7

6

12

67

0

1

2

3

4

5

6

7

8

Front=5Rear=

docsity.com