You are on page 1of 7

Data Structures in C++ Note 3

Note 3: Stack and Queue Concept in Data Structure for Application


Stack:
It is a sequence of items that are accessible at only one end of the sequence. Think of a
stack as a collection of items that are piled one on top of the other, ith access limited to
the topmost item. ! stack inserts item on the top of the stack and remo"es item from the
top of the stack. It has #I$% &last'in ( first'out) orderin* for the items on the stack.
Type of Stack:
#inear Stack
#inked #ist Stack
Operation of Stack:
!dd+ a push& ) operation adds an item to the topmost location on the stack.
top = 3
add 4 to the
stack
top = 2
top = 3
stack:
increase top
1
2
3
push 4 to the stack
stack:
1
2
3
4
stack:
1
2
3
,ush function+
"oid push & short stack-., short stack/si0e, short top, short item)
1
if & top 23 stack/si0e 45)
1
cout 66 7The stack is full89 66 endl:
return:
;
stack-+ + top. 3 item:
;
5<
Data Structures in C++ Note 3
Delete+ a pop& ) operation remo"es an item from the topmost location on the stack
pop 4 out of the stack
delete 4 from
the stack
top = 3
stack:
decrease top
1
2
3
top = 3
top = 2
stack:
1
2
3
stack:
2
4
1
3
,op function+
short pop &short stack-., short stack/si0e, short top)
1
if & top 3 3 45)
1
cout 66 7The stack is empty89 66 endl:
return =:
;
return stack-top 4 4 . :
;
Queue:
! queue is a sequential stora*e structure that permits access only at the to ends of the
sequence. >e refer to the ends of the sequence as the front and rear. ! queue inserts
ne elements at the rear and remo"es elements from the front of the sequence. ?ou ill
note that a queue remo"es elements in the same order in hich they ere stored, and
hence a queue pro"ides $I$% &first'in ( first'out), or $C$S &first'come ( first'ser"ed),
orderin*.
Type of the Queue:
#inear @ueue+ non'circular queue, circular queue, priority queue
#inked #ist @ueue+ non'circular queue, circular queue, priority queue
Operation of Queue:
!dd+ insert operation for the queue is addin* item to the ne element at the rear of queue.
Delete+ remo"e operation for the queue is deletin* item from the front element of queue.
The non'circular queue ith A elements+
5B
Data Structures in C++ Note 3
!l*orithms+
Cariables+
short qfront 3 '5, qrear 3 '5, qsi0e 3 A:
Insert+
qrear 3 qsi0e'5
and
qfront 3 '5
queue
is full
return
5
qrear 3 qsi0e'5
shift all elements left so
that 5st element is at =
qfront 3 '5
qrear++
queue-qrear. 3 "alue
return =
T $
F T
Delete+
qfront 3 qrear
queue
is
empty
qfront 3 '5
qrear 3 '5
return 5
qfront++
"alue 3 queue-qfront.
return =
T $
Draphical ,resentation+
5 = E 3 < B F G
qfront qrear
The circular queue ith A elements+
!l*orithms+
Cariables+
short qfront 3 =, qcount 3 =, qsi0e 3 A:
Insert+
5F
Data Structures in C++ Note 3
qcount 3 qsi0e
queue
is full
return
5
qrear 3 &qfront+qcount)H qsi0e
queue-qrear. 3 "alue
qcount++
return =
T
$
Delete+
qcount 3 =
queue
is
empty
return
5
"alue 3 queue-qfront.
qfront 3 &qfront+5)H qsi0e
qcount ' '
return =
T
$
Draphical ,resentation+
5
E
3
<
B
F
G
qfront
qrear
=
Stacks and @ueue Structure Table
Structure
Type
!rray #ink #ist #ink #ist !rray
Stacks #inear Stacks #inear Stacks #inear Stacks
@ueue Non'Circular @ueue
Circular @ueue
,riority @ueue
Non'Circular @ueue
Circular @ueue
,riority @ueue
Non'Circular @ueue
Circular @ueue
,riority @ueue
Multiple Stacks and Queues:
5G
Data Structures in C++ Note 3
Multiple Stacks:
$olloin* pictures are to ays to do to stacks in array+
5. None fiIed si0e of the stacks+
Graphical Picture: ithout fiIed si0e of stack
Stack 1 Stack 2
-1 -2 0 1 2 3 4 5 6 7 8 10 11 12
!rray/ptr
12 0
S
t
a
c
k

1

!
o
p
S
t
a
c
k

2

!
o
p
Stack 5 eIpands from the =
th
element to the ri*ht
Stack E eIpands from the 5E
th
element to the left
!s lon* as the "alue of Top5 and TopE are not neIt to each other, it has free
elements for input the data in the array
>hen both Stacks are full, Top5 and Top E ill be neIt to each other
There is no fiIed boundary beteen Stack 5 and Stack E
Jlements 45 and 4E are usin* to store the information needed to manipulate the
stack &subscript for Top 5 and Top E)
E. $iIed si0e of the stacks+
S
t
a
c
k

1

!
o
p
Stack 1 Stack 2
!rray/ptr
Graphical Picture: ith fiIed si0e of stack
-3 -4 -2 -1 0 1 2 3 4 5 6 7 8
6 5
10
0 6
S
t
a
c
k

2

!
o
p
S
t
a
c
k

1

S
i
"
e
S
t
a
c
k

2

S
i
"
e
Stack 5 eIpands from the =
th
element to the ri*ht
Stack E eIpands from the F
th
element to the left
!s lon* as the "alue of Top 5 is less than F and *reater than =, Stack 5 has free
elements to input the data in the array
5A
Data Structures in C++ Note 3
!s lon* as the "alue of Top E is less than 55 and *reater than B, Stack E has free
elements to input the data in the array
>hen the "alue of Top 5 is B, Stack 5 is full
>hen the "alue of Top E is 5=, stack E is full
Jlements 45 and 4E are usin* to store the si0e of Stack 5 and the subscript of the
array for Top 5 needed to manipulate Stack 5
Jlements 43 and 4< are usin* to store the si0e of Stack E and the subscript of the
array for Top E needed to manipulate Stack E
Multiple Queues:
$olloin* pictures are to ays to do to queues in array+
5. None fiIed si0e of the queues+
Graphical Picture: ithout fiIed si0e of the queue
@ueue 5 @ueue E
!rray/ptr
@
u
e
u
e

E

$
r
o
n
t
@
u
e
u
e

E

S
i
0
e
-5 -6 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 5 0 0
8
4 5
@
u
e
u
e

5

S
i
0
e
@
u
e
u
e

5

$
r
o
n
t
@
u
e
u
e

E

C
o
u
n
t
@
u
e
u
e

5

C
o
u
n
t
Temporary
Koundary
@ueue 5 eIpands from the =
th
element to the ri*ht and circular back to the =
th

element
@ueue E eIpands from the A
th
element to the left and circular back to the A
th

element
Temporary boundary beteen the @ueue 5 and the @ueue E: as lon* as there has
free elements in the array and boundary ould be shift
$ree elements could be any here in the @ueue such as before the front, after the
rear, and beteen front and rear in the @ueue
@ueue 5Ls and @ueue E Ms si0e could be chan*e if it is necessary. >hen the @ueue
5 is full and the @ueue E has free space: the @ueue 5 can increase the si0e to use
that free space from the @ueue E. Same ay for the @ueue E
Jlements 45, 4E, and 43 are usin* to store the si0e of the @ueue 5, the front of the
@ueue 5, and the data count for the @ueue 5 needed to manipulate the @ueue 5
Jlements 4<, 4B, and 4F are usin* to store the si0e of the @ueue E, the front of the
@ueue E, and the data count for the @ueue E needed to manipulate the @ueue E
Inserts data to the @ueue 5, @5Near 3 &@5$ront + @5count) H @5Si0e
5O
Data Structures in C++ Note 3
Inserts data to the @ueue E, @ENear 3 &@E$ront + @Ecount) H @ESi0e + @5Si0e
Deletes data from the @ueue 5, @5$ront 3 &@5$ront + 5) H @5Si0e
Deletes data from the @ueue E, @E$ront 3 &@E$ront + 5) H @ESi0e + @5Si0e
E. $iIed si0e of the queue+
Graphical Picture: ith fiIed si0e of the queue
@ueue 5 @ueue E
!rray/ptr
@
u
e
u
e

E

$
r
o
n
t
@
u
e
u
e

E

C
o
u
n
t
Koundary beteen
the @ueues
-5 -6 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 5 0 0
8
4 5
@
u
e
u
e

5

C
o
u
n
t
@
u
e
u
e

5

$
r
o
n
t
@
u
e
u
e

E

S
i
0
e
@
u
e
u
e

5

S
i
0
e
@ueue 5 eIpands from the =
th
element to the <
th
element and circular back to =
th

element
@ueue E eIpands from the A
th
element to the B
th
element and circular back to A
th

element
The boundary is fiIed beteen the @ueue 5 and the @ueue E
$ree elements could be any here in the @ueue such as before the front, after the
rear, and beteen front and rear in the @ueue
Jlements 45, 4E, and 43 are usin* to store the si0e of the @ueue 5, the front of the
@ueue 5, and the data count for the @ueue 5 needed to manipulate the @ueue 5
Jlements 4<, 4B, and 4F are usin* to store the si0e of the @ueue E, the front of the
@ueue E, and the data count for the @ueue E needed to manipulate the @ueue E
Inserts data to the @ueue 5, @5Near 3 &@5$ront + @5count) H @5Si0e
Inserts data to the @ueue E, @ENear 3 &@E$ront + @Ecount) H @ESi0e + @5Si0e
Deletes data from the @ueue 5, @5$ront 3 &@5$ront + 5) H @5Si0e
Deletes data from the @ueue E, @E$ront 3 &@E$ront + 5) H @ESi0e + @5Si0e
E=

You might also like