C++ Programming: Program Design Including Data Structures,...

28
Lesson 4 Stacks Readings: chapter 18

Transcript of C++ Programming: Program Design Including Data Structures,...

Page 1: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Lesson 4

Stacks

Readings: chapter 18

Page 2: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Stacks

• Stack: a data structure in which elements are added and removed from one end only

– Addition/deletion occur only at the top of the stack

– Last in first out (LIFO) data structure

• Operations:

– Push: to add an element onto the stack

– Pop: to remove an element from the stack

– isEmpty: to check if the stack is empty

CSE 143 2

Page 3: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Stacks (cont’d.)

CSE 143 3

Page 4: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Stacks (cont’d.)

4 CSE 143

Page 5: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Implementation of Stacks as Arrays

• First element goes in first array position, second in the second position, etc.

• Top of the stack is index of the last element added to the stack

• Stack elements are stored in an array, which is a random access data structure – Stack element is accessed only through top

• To track the top position, use a variable called stackTop

CSE 143 5

Page 6: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Implementation of Stacks as Arrays

• The stack elements are stored in an array

– The stack uses a prefix of the array

– The size of the array is the maximum number of elements in the stack

• class stackType implements the functions of the abstract class stackADT

CSE 143 6

Page 7: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Implementation of Stacks as Arrays

• C++ arrays begin with the index 0

• If stackTop is 0, stack is empty

• If stackTop is nonzero, stack is not empty

– Top element is given by stackTop – 1

– stackTop is the size of the stack

CSE 143 7

Page 8: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Implementation of Stacks as Arrays

CSE 143 8

Page 9: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Initialize Stack

CSE 143 9

Page 10: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Push

• Store the newItem in the array component indicated by stackTop

• Increment stackTop

• Overflow occurs if we try to add a new item to a full stack

CSE 143 10

Page 11: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Push

CSE 143 11

Page 12: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Push

CSE 143 12

Page 13: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Return the Top Element

• Returns the top element of the stack

• Does not change the state of the stack

• Overflow condition: only allowed if the stack is not empty

CSE 143 13

Page 14: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Pop

• Removes the top element and returns it

• To remove an element from the stack, decrement stackTop by 1

• Underflow condition: trying to remove an item from an empty stack

CSE 143 14

Page 15: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Pop

CSE 143 15

Page 16: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Pop

CSE 143 16

Page 17: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Constructor and Destructor

• Constructor:

– Creates array to store stack elements (with default array size)

– Sets stackTop to 0

– Sets stack size to 0

• Destructor:

– Deallocates memory occupied by the array

– Sets stackTop to 0

CSE 143 17

Page 18: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Stack Header File

• Place definitions of class and function profiles (stack operations) together in a file

– called ArrayStack.h

• Place implementation of class and function bodies together in a file

– called ArrayStack.cpp

CSE 143 18

Page 19: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Linked Implementation of Stacks

• Array only allows fixed number of elements

• If number of elements to be pushed exceeds array size, the program may terminate

• Linked lists can dynamically organize data

• In a linked representation, stackTop is pointer to memory address of top element in stack

CSE 143 19

Page 20: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Linked Implementation of Stacks

CSE 143 20

Page 21: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Default Constructor

• Initializes the stack to an empty state when a stack object is declared

– Sets stackTop to NULL

– Sets size to 0

CSE 143 21

Page 22: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Empty Stack and Full Stack

• In a linked implementation of stacks, function isFullStack does not apply

– Logically, the stack is never full

• Stack is empty if stackTop is NULL

CSE 143 22

Page 23: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Initialize Stack

• initializeStack: reinitializes stack to an empty state

– Must deallocate memory occupied by current element

– Sets stackTop to NULL

CSE 143 23

Page 24: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Push

• newNode is added at the beginning of the linked list pointed to by stackTop

CSE 143 24

Page 25: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Push

CSE 143 25

Page 26: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Return the Top Element

• top operation

– returns top element of stack

– returns the element in the node pointed by stackTop

CSE 143 26

Page 27: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Pop

• Node pointed to by stackTop is removed

– Second element becomes top element

CSE 143 27

Page 28: C++ Programming: Program Design Including Data Structures, …users.polytech.unice.fr/~gaetano/mydropbox/cse143/slides/... · 2017-03-30 · a random access data structure –Stack

Pop

CSE 143 28