You are on page 1of 7

QUEUE

↳ Fifo

( First In First Out )

Queueabt

Data i) Space for elements


storing
:

(2) Front :
for deletion

Rear
(3) :
for insertion

Operations ( II en in )
:
queue
(2) dequeue C )

(3) is Empty C)

(4) is Full C )

(5)
first C )
(6) last C)

(1)
Array
(2) Linked List

QUEUEUSARRAY
I Queue
.

using single pointer


2. Queue
using front and rear .

Drawbacks of
queue using array
3 .
.

Oueueusingsinglepointerv
I .

O l Z 3 4

j t
R
5

Insert -
OCI )

Delete - Ocn )
2 .

Oueueusingfoontandrear

initially front --
rear = t
-

O l Z 3 4
T T
F R

Insertion : increment rear and insert Oci )

Deletion delete Ol ' )


: increment
front and

Empty : if (
front ==
rear )

Full : if ( year = size -


l )

PROGRAMFORQVEVE.US#ARRAY

Struct Queue
{
int size ;

or
int front ;
size
int rear ;

int re
Q ; front
} rear

,
O l Z 3 . .
.

int main C )

{
Struct Queue g ;
);
"

print fl Size
"
Enter :

scarf l
''

Sg
"
t
.
d ,
- size ) ;
O int size ] ;
Eq
=
new
g.
-

dequeue
*
g front =

g. rear
= l ; void ( Queue q)
- -

} {

int n = -
l ;
*
void en
queue ( Queue q ,
int n)

{ if ( →
front == → rear )
q q
point fl
"
if ( → = = → size i)
"
Queue is empty ) ;
q rear
q
-
-

);
"
Queue
"

print fl is
full else

else {
{
q→ front
-1T
;

q
→ reartt
; n=q→O[ q →
front ] ;
→ a Eq → rear ) n }
q ;
-

} return u ;
} 3
DRAWBACKOFQUEUF.US#ARRAY
117/-15/8/6191
-

I .
we cannot utilize space of deleted element .

3 5
O l Z 4
T T
F R
Every location can be used
2 .

only once

3 . A situation where
queue
is empty and full
/
O l Z 3 4 5

AT
F R
USlNhSPACEAGAINS0LUT#

(1) Resetting
whenever and Rear at initialize
Front are
pointing same place them as I
-

(2) Ciowlarclueve In circular queue , front and rear

initialize with
array's first position .

void enquevec struct queue


*
int n)
q ,

{
if ( ( Rear + 1) To if front
size
front ) check rear's neat is
→ y →
q
.

g

==q
pzintfl
"
Queue is Full
''
l
;
else

{
Rear (g 1) t size
q→
→ seas →
+
q ;
= -

q
→ a
Eq → rear ]=n ;
}
} rear = ( rear -11 ) t .
size
O
(0+1)%7 =
1

I
(1+1) y .
7 = 2

Void 2 (2+1)%7
dequeue ( struct * =
q)
Queue 3

{ 3 (3+1)%7 = 4

int N= - I 4
(4+1)%7 =
5

5 (5tDY
if ( rear) 7 6
front
→ →
q
=
q
= - .

print fl
"
Queue is Empty ) ;
"
6 (6+1)%7 = 0

else

q→ front
-_
(q →
front -11 ) t .

q
→ size ;

n =
q→OEq→ front] ;
g

return n ;
}
QUEUE.US/NGLlNKEDL#
-

Empty : if ( front ==
NULL )

*
Fun : Node t =
new Node ;

/ J KNO
if It -_ =

nodes
NULL)

be
FRONT
more can created
REAR

i. e heap is full

void enqueue
( int m) O ( i)
{
*
Node t =
new Node ;

if ( t ==
NULL )

);
printfl
"
"
Queue is Full

else

{
+ → data =u
;
t → neat =
NULL ;

if ( front == NULL)

front - year = t ;

else
{

years → neut = t

rear =t ;

}
}
}

int dequeuec ) Oli)

{
int n= -
l
;
*
Node p ;
if I
front =
NULL )
-
-

" "
);
print fl Queue is
Empty
-

else

p= front ;
front =
front →
hent ;

u =p → data ;
free ( p ) ;
}
return n ;
}
Dtclueue

↳ Double Ended Queue

QUEUE DEOUEUE

# #
Insert Delete Insert Delete

front front r
V X r
rear rear

INPUT RESTRICTED DEOUEUE OUTPUT RESTRICTED DEOUEUE

# #
Insert Delete Insert Delete

front front
✓ ✓ r x
rear rear

PRIORITYQUEVESI
.
Limited set of priorities .

2 . Element Priority

1 .

limitedsetofpoiooiti.es#

Priorities =3

Element → A B C D E F G H I J

Priority → l l 2 3 2 I 2 3 2 2

Psiorityclueves DELETION

)
For deletion O
,
elements
of ,
will be
01 deleted first ,
then Oz and then 03

a Fetal elements win be deleted in ,= , .ro

03

INSERTION
2 . Element

Priority

→ 5
Elements 6,8 ,
3 , 10 , 15,2 ,
9
,
17 , , 8


Element is itself a
priority

Smaller number higher priority

1 . Insert in same order 011)

Delete it Oln)
man
priority by searching

Insert order of Oln )


2 .
in
increasing priority
Delete the last element of array Oct )

QUEUEUS.in#STACks assume stacks to be

implemented Using linked

en ( int n) list
queue
{

push ( ISI ,
n ) ;

} -

deqeeuec ,
int
int n = -
l
;
SL S2
if l is Empty ( S2 ) )

{
if l is
Empty S1 )

{
"

print fl Queue Empty );


"

return n ;

else

{
While l ! is Empty ( Sl ) )

( d S2 ( ASI ) ) ;
push ,
pop
}
}
return
pop ( d S2)
;
}

You might also like