Chapter 7 Stacks 1 Outline Stacks Further Stack

  • Slides: 22
Download presentation
Chapter 7 Stacks 1

Chapter 7 Stacks 1

Outline Stacks Further Stack Examples Pushing/Popping a Stack Class Stack Static stack class Dynamic

Outline Stacks Further Stack Examples Pushing/Popping a Stack Class Stack Static stack class Dynamic stack class STL list Stack applications 2

Stacks n A stack is a sequence of items that are accessible at only

Stacks n A stack is a sequence of items that are accessible at only one end of the sequence. 3

Further Stack Examples 4

Further Stack Examples 4

Pushing/Popping a Stack l Because a pop removes the item last added to the

Pushing/Popping a Stack l Because a pop removes the item last added to the stack, we say that a stack has LIFO (lastin/first-out) ordering. 5

§- Stack - Storage Structure with insert (push) and erase (pop) operations occur at

§- Stack - Storage Structure with insert (push) and erase (pop) operations occur at one end, called the top of the stack. - The last element in is the first element out of the stack, so a stack is a LIFO structure. 6 6

CLASS stack Constructor <stack> Operations <stack> stack(); Create an empty stack CLASS stack bool

CLASS stack Constructor <stack> Operations <stack> stack(); Create an empty stack CLASS stack bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. 7

CLASS stack Operations <stack> void pop(); Remove the item from the top of the

CLASS stack Operations <stack> void pop(); Remove the item from the top of the stack. Precondition: The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top. 8

CLASS stack Operations <stack> int size() const; Return the number of items on the

CLASS stack Operations <stack> int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition: The stack is not empty. const T& top() const; Constant version of top(). 9

Using a Stack to Create a Hex Number 10

Using a Stack to Create a Hex Number 10

Uncoupling Stack Elements 11

Uncoupling Stack Elements 11

Uncoupling Stack Elements 12

Uncoupling Stack Elements 12

Uncoupling Stack Elements 13

Uncoupling Stack Elements 13

Uncoupling Stack Elements 14

Uncoupling Stack Elements 14

Uncoupling Stack Elements 15

Uncoupling Stack Elements 15

Uncoupling Stack Elements 16

Uncoupling Stack Elements 16

Stack class Static stack class (built on array): P 1074 Dynamic stack class (built

Stack class Static stack class (built on array): P 1074 Dynamic stack class (built on linked list): P 1085 STL stack: d_stack. h 17

§- Recursion - The system maintains a stack of activation records that specify: 1)

§- Recursion - The system maintains a stack of activation records that specify: 1) the function arguments 2) the local variables/objects 3) the return address - The system pushes an activation record when calling a function and pops it when returning. 18 18

19

19

§- Postfix/RPN Expression Notation - places the operator after its operands - easy to

§- Postfix/RPN Expression Notation - places the operator after its operands - easy to evaluate using a single stack to hold operands. - The rules: 1) Immediately push an operand onto the stack. 2) For a binary operator, pop the stack twice, perform the operation, and push the result onto the stack. 3) 20 At the end a single value remains on the stack. This is the value of the expression. 20

21

21

§- Infix notation - A binary operator appears between its operands. - More complex

§- Infix notation - A binary operator appears between its operands. - More complex than postfix, because it requires the use of operator precedence and parentheses. - In addition, some operators are left-associative, and a few are right-associative. 22 22