Chapter 18

:
Stacks And Queues

Copyright © 2012 Pearson Education, Inc.

18.1
Introduction to the Stack ADT

Copyright © 2012 Pearson Education, Inc.

first out data structure • !"a#$les: % $lates in a cafeteria % return addresses for function calls • I#$le#entation: % static: fi"ed si&e. . Inc. i#$le#ented as linked list Copyright © 2012 Pearson Education.Introduction to the Stack ADT • Stack: a LIFO (last in. i#$le#ented as arra' % d'na#ic: (aria)le si&e.

.A LIFO Structure Copyright © 2012 Pearson Education. Inc.

.Stack O$erations and Functions • O$erations: % $ush: add a (alue onto the to$ of the stack % $o$: re#o(e a (alue fro# the to$ of the stack • Functions: – isFull: true if the stack is currentl' full. has no #ore s$ace to hold additional ele#ents % is!#$t': true if the stack currentl' contains no ele#ents Copyright © 2012 Pearson Education.. i. Inc.e.

E E Copyright © 2012 Pearson Education.Stack O$erations * !"a#$le • A stack that can hold char (alues: G K push('E'). E push('K'). . Inc. K push('G').

Stack O$erations * !"a#$le • A stack that can hold char (alues: K pop(). (re#o(e K E pop(). . Inc. (re#o(e E Copyright © 2012 Pearson Education. (re#o(e G E pop().

.(See IntStack. Inc.cpp for the implementation.) Copyright © 2012 Pearson Education.

+ D'na#ic Stacks Copyright © 2012 Pearson Education.18. . Inc.

ro.and shrink as necessar' • . Inc.an/t e(er )e full as lon0 as #e#or' is a(aila)le • I#$le#ented as a linked list Copyright © 2012 Pearson Education.D'na#ic Stacks • . .

. • .an also use the i#$le#entation of stack a(aila)le in the STL Copyright © 2012 Pearson Education. Inc.I#$le#entin0 a Stack • 1ro0ra##ers can $ro0ra# their o-n routines to i#$le#ent stack functions • See DynIntStack class in the )ook for an e"a#$le.

Inc.ontainer Copyright © 2012 Pearson Education.2 The STL stack .18. .

pop. . a linked list. or a deque • I#$le#ents push. Inc.The STL stack container • Stack te#$late can )e i#$le#ented as a vector. and e pty #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 Copyright © 2012 Pearson Education.

• i#$le#ented usin0 a list: stack" char# list"char$ $ cstack. na#ed cstack. "" s'#)ols Copyright © 2012 Pearson Education. • i#$le#ented usin0 a deque: stack" char $ cstack. .Definin0 a stack • Definin0 a stack of chars. • S$aces are re3uired )et-een consecuti(e $$. i#$le#ented usin0 a vector: stack" char# vector"char$ $ cstack. Inc.

18.4 Introduction to the 5ueue ADT Copyright © 2012 Pearson Education. . Inc.

• !"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. Inc.Introduction to the 5ueue ADT • 5ueue: a FIFO (first in. i#$le#ented as arra' % d'na#ic: (aria)le si&e. first out data structure. i#$le#ented as linked list Copyright © 2012 Pearson Education. .

5ueue Locations and O$erations • rear: $osition -here ele#ents are added • front: $osition fro# -hich ele#ents are re#o(ed • en3ueue: add an ele#ent to the rear of the 3ueue • de3ueue: re#o(e an ele#ent fro# the front of a 3ueue Copyright © 2012 Pearson Education. Inc. .

5ueue O$erations * !"a#$le • A currentl' e#$t' 3ueue that can hold char (alues: % enqueue('E'). . front E rear Copyright © 2012 Pearson Education. Inc.

. front E K G rear Copyright © 2012 Pearson Education.5ueue O$erations * !"a#$le % enqueue('K'). front K rear % enqueue('G'). Inc.

&& re ove E front K G rear % dequeue(). Inc.5ueue O$erations * !"a#$le % dequeue(). && re ove K front G rear Copyright © 2012 Pearson Education. .

and also let rear inde" 9-ra$ around9 to front of arra'. . Inc. de3ueue code Copyright © 2012 Pearson Education. re#ainin0 ele#ents #ust shift to front • Solutions: % Let front inde" #o(e as ele#ents are re#o(ed (-orks as lon0 as rear inde" is not at end of arra' % 8se a)o(e solution. treatin0 arra' as circular instead of linear (#ore co#$le" en3ueue. Solutions • 7hen re#o(in0 an ele#ent fro# a 3ueue.dequeue Issue.

oints to the queue array < int queueSi!e. && .-). Inc. && Su>script o( the queue (ront '' int rear. && Su>script o( the queue rear '* int nu Ite s.u >er o( ite s in the queue 18*++ Copyright © 2012 Pearson Education. && .h ' && Speci(ication (ile (or the Int)ueue class * +i(nde( I.E. .E/0 1 +de(ine I.E/0 2 3 class Int)ueue 4 5 6 private7 8 int 9queue:rray.Contents of IntQueue.-).E. && -he queue si!e '= int (ront.

ontents of Int)ueueCh (. >ool isE pty() const. && Destructor AInt)ueue().ontinued '1 '2 '3 '4 '6 '8 '< *= *' ** *1 *2 *3 *4 *6 *8 *< 1= pu>lic7 && ?onstructor Int)ueue(int).c$$ for the i#$le#entation Copyright © 2012 Pearson Education. +endi( (See Int5ueue. void clear(). . && ?opy constructor Int)ueue(const Int)ueue @). B. void dequeue(int @). Inc. >ool isFull() const.. && )ueue operations void enqueue(int).

18.: D'na#ic 5ueues Copyright © 2012 Pearson Education. . Inc.

. . Inc.DD front rear Copyright © 2012 Pearson Education. a 3ueue can )e i#$le#ented usin0 a linked list • Allo-s d'na#ic si&in0.D'na#ic 5ueues • Like a stack. a(oids issue of shiftin0 ele#ents or -ra$$in0 indices .

an also use the i#$le#entation of 3ueue and de3ueue a(aila)le in the STL Copyright © 2012 Pearson Education. Inc. .I#$le#entin0 a 5ueue • 1ro0ra##ers can $ro0ra# their o-n routines to i#$le#ent 3ueue o$erations • See the DynInt)ue class in the )ook for an e"a#$le of a d'na#ic 3ueue • .

.ontainers Copyright © 2012 Pearson Education. The STL deque and queue .18. . Inc.

Inc. or deque. . <as #e#)er functions to en3ueue (push/>ack and de3ueue (pop/(ront % queue: container ADT that can )e used to $ro(ide 3ueue as a vector.ontainers % deque: a dou)le*ended 3ueue. <as #e#)er functions to en3ue ( push and de3ueue (pop Copyright © 2012 Pearson Education. list.The STL deque and queue .

i#$le#ented usin0 a de3ue: deque"char$ c)ueue. na#ed c5ueue.Definin0 a queue • Definin0 a 3ueue of chars. • i#$le#ented usin0 a list: queue" char# list"char$ $ c)ueue. Inc. • S$aces are re3uired )et-een consecuti(e $$. • i#$le#ented usin0 a 3ueue: queue"char$ c)ueue. . "" s'#)ols Copyright © 2012 Pearson Education.