8/13/2019 Chapter 18 Stacks and Queues
1/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Chapter 18:
Stacks And
Queues
8/13/2019 Chapter 18 Stacks and Queues
2/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.1Introduction to the
Stack ADT
8/13/2019 Chapter 18 Stacks and Queues
3/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Introduction to the Stack ADT
Stack: a LIFO (last in, first out datastructure
!"a#$les:
% $lates in a cafeteria% return addresses for function calls
I#$le#entation:
% static: fi"ed si&e, i#$le#ented as arra'% d'na#ic: aria)le si&e, i#$le#ented as linkedlist
8/13/2019 Chapter 18 Stacks and Queues
4/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
A LIFO Structure
8/13/2019 Chapter 18 Stacks and Queues
5/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Stack O$erations and Functions
O$erations:
% $ush: add a alue onto the to$ of the stack
% $o$: re#oe a alue fro# the to$ of the stack Functions:
isFull: trueif the stack is currentl' full, i.e.,
has no #ore s$ace to hold additional ele#ents% is!#$t': trueif the stack currentl' contains
no ele#ents
8/13/2019 Chapter 18 Stacks and Queues
6/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Stack O$erations * !"a#$le
A stack that can hold charalues:
K
E
G
K
EEpush('E'); push('K'); push('G');
8/13/2019 Chapter 18 Stacks and Queues
7/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Stack O$erations * !"a#$le
A stack that can hold charalues:
E
K
Epop();(re#oe Gpop();(re#oe K
pop();(re#oe E
8/13/2019 Chapter 18 Stacks and Queues
8/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
(See IntStack.cpp for the
implementation.)
8/13/2019 Chapter 18 Stacks and Queues
9/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.+D'na#ic Stacks
8/13/2019 Chapter 18 Stacks and Queues
10/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
D'na#ic Stacks
ro- and shrink as necessar'
an/t eer )e full as lon0 as #e#or' is
aaila)le I#$le#ented as a linked list
8/13/2019 Chapter 18 Stacks and Queues
11/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
I#$le#entin0 a Stack
ro0ra##ers can $ro0ra# their o-nroutines to i#$le#ent stack functions
See DynIntStackclass in the )ook foran e"a#$le.
an also use the i#$le#entation of stackaaila)le in the STL
8/13/2019 Chapter 18 Stacks and Queues
12/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.2The STL stackontainer
8/13/2019 Chapter 18 Stacks and Queues
13/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
The STL stackcontainer
Stack te#$late can )e i#$le#ented as avector, a linked list, or a deque
I#$le#entspush
,pop
, andepty
#e#)er functions
I#$le#ents other #e#)er functions: si!e: nu#)er of ele#ents on the stack
top: reference to ele#ent on to$ of the stack
8/13/2019 Chapter 18 Stacks and Queues
14/29Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Definin0 a stack
Definin0 a stack of chars, na#ed cstack,i#$le#ented usin0 a vector:stack" char# vector"char$ $ cstack;
i#$le#ented usin0 a list:stack" char# list"char$ $ cstack;
i#$le#ented usin0 a deque:stack" char $ cstack;
S$aces are re3uired )et-een consecutie $$,""s'#)ols
8/13/2019 Chapter 18 Stacks and Queues
15/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.4Introduction to the 5ueue ADT
8/13/2019 Chapter 18 Stacks and Queues
16/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Introduction to the 5ueue ADT
5ueue: a FIFO (first in, first out data structure.
!"a#$les:% $eo$le in line at the theatre )o" office
% $rint 6o)s sent to a $rinter
I#$le#entation:% static: fi"ed si&e, i#$le#ented as arra'
% d'na#ic: aria)le si&e, i#$le#ented as linked list
8/13/2019 Chapter 18 Stacks and Queues
17/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
5ueue Locations and
O$erations
rear: $osition -here ele#ents are added
front: $osition fro# -hich ele#ents are
re#oed en3ueue: add an ele#ent to the rear of
the 3ueue
de3ueue: re#oe an ele#ent fro# thefront of a 3ueue
8/13/2019 Chapter 18 Stacks and Queues
18/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
5ueue O$erations * !"a#$le
A currentl' e#$t' 3ueue that can hold charalues:
% enqueue('E');
E
front
rear
8/13/2019 Chapter 18 Stacks and Queues
19/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
5ueue O$erations * !"a#$le
% enqueue('K');
% enqueue('G');
K
E K G
front
rear
front
rear
8/13/2019 Chapter 18 Stacks and Queues
20/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
5ueue O$erations * !"a#$le
% dequeue(); && reove E
% dequeue(); && reove K
K G
Gfront
rear
front
rear
8/13/2019 Chapter 18 Stacks and Queues
21/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
dequeueIssue, Solutions
7hen re#oin0 an ele#ent fro# a 3ueue,re#ainin0 ele#ents #ust shift to front
Solutions:% Let front inde" #oe as ele#ents are re#oed (-orks
as lon0 as rear inde" is not at end of arra'
% se a)oe solution, and also let rear inde" 9-ra$around9 to front of arra', treatin0 arra' as circularinstead of linear (#ore co#$le" en3ueue, de3ueuecode
8/13/2019 Chapter 18 Stacks and Queues
22/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.18*++
Contents of IntQueue.h && Speciication ile or the Intueue class
* +inde I,-.E.E/01 +deine I,-.E.E/0
2
3 class Intueue
4 5
6 private7
8 int 9queue:rray; && oints to the queue array< int queueSi!e; && -he queue si!e
= int ront; && Su>script o the queue ront int rear; && Su>script o the queue rear* int nuItes; && ,u>er o ites in the queue
8/13/2019 Chapter 18 Stacks and Queues
23/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
1 pu>lic7
2 && ?onstructor3 Intueue(int);
46 && ?opy constructor
8 Intueue(const Intueue @);
ool isEpty() const;
*6 >ool isFull() const;
*8 void clear();
*< B;
1= +endi
(See Int5ueue.c$$ for the
i#$le#entation
ontents of IntueueCh
(ontinued
8/13/2019 Chapter 18 Stacks and Queues
24/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.D'na#ic 5ueues
8/13/2019 Chapter 18 Stacks and Queues
25/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
D'na#ic 5ueues
Like a stack, a 3ueue can )e i#$le#ented
usin0 a linked list
Allo-s d'na#ic si&in0, aoids issue of
shiftin0 ele#ents or -ra$$in0 indices
front rear
,.
8/13/2019 Chapter 18 Stacks and Queues
26/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
I#$le#entin0 a 5ueue
ro0ra##ers can $ro0ra# their o-n
routines to i#$le#ent 3ueue o$erations
See the DynIntueclass in the )ook foran e"a#$le of a d'na#ic 3ueue
an also use the i#$le#entation of 3ueue
and de3ueue aaila)le in the STL
8/13/2019 Chapter 18 Stacks and Queues
27/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
18.;The STL dequeand queue
ontainers
8/13/2019 Chapter 18 Stacks and Queues
28/29
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
The STL deque
and queueontainers
% deque: a dou)le*ended 3ueue. ack and de3ueue (pop/ront% queue: container ADT that can )e used to
$roide 3ueue as a vector, list, or deque.
8/13/2019 Chapter 18 Stacks and Queues
29/29
C i h 2012 d iC i h 2012 d i
Definin0 a queue
Definin0 a 3ueue of chars, na#edc5ueue, i#$le#ented usin0 a de3ue:deque"char$ cueue;
i#$le#ented usin0 a 3ueue:queue"char$ cueue;
i#$le#ented usin0 a list:
queue" char# list"char$ $ cueue; S$aces are re3uired )et-een consecutie$$, ""s'#)ols