Stacks Data Structures and Problem Solving with C

Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Introduction to the Stack ADT • Stack: a LIFO (last in, first out) data structure • Examples: • plates in a cafeteria serving area • return addresses for function calls 18 -2

Specifications for the ADT Stack • A stack of cafeteria plates Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Abstract Data Type: Stack • A finite number of objects • Not necessarily distinct • Having the same data type • Ordered by when they were added • Stack uses LIFO principle • Last In First Out • We have identified the following operations: • • See whether stack is empty. Add a new item to stack. Remove from the stack item added most recently. Get item that was added to stack most recently. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Stack Abstract Data Type Functions: • push: add a value at the top of the stack • pop: remove a value from the top of the stack • peek: show the top of the stack, but don’t remove it. • is. Empty: true if the stack currently contains no elements 18 -5

Stack Basics • Stack is usually implemented as a list, with additions and removals taking place at one end of the list • The active end of the list implementing the stack is the top of the stack • Stack types: • Static – fixed size, often implemented using an array • Dynamic – size varies as needed, often implemented using a linked list 18 -6

An Array Based Implementation FIGURE 7 -1 Using an array to store a stack’s entries: (a) a preliminary sketch; (b) implementation details Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Static Stack Implementation • Uses an array of a fixed size • Bottom of stack is at index 0. A variable called top tracks the current top of the stack const int STACK_SIZE = 3; char s[STACK_SIZE]; int top = 0; top is where the next item will be added 18 -8

Array Implementation Example This stack has max capacity 3, initially top = 0 and stack is empty. G K push('E'); top is 1 E push('K'); top is 2 E K push('G'); top is 3 E 18 -9

Stack Operations Example After three pops, top is 0 and the stack is empty pop(); (remove G) K E pop(); (remove K) E pop(); (remove E) 18 -10

Stack Operations Example Push(3) Push(4) Peek() Pop() top 18 -11

Class Implementation class STACK { private: char *s; int capacity, top; public: void push(char x); char pop(); char peek(); bool is. Empty(); STACK(int stack. Size); STACK(); ~STACK() }; int Stack. Is. Empty = 1009; int Stack. Full. Exception = 1010; 18 -12
![Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool is. Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool is.](http://slidetodoc.com/presentation_image_h2/5ac9c3b323681ecba294ac552434b29d/image-13.jpg)
Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool is. Empty() { if (top == 0) return true; else return false; } 18 -13

Array Implementation To add an item to the stack void push(char x) { if (top == STACK_SIZE()) {throw(Stack. Full. Exception); } // throw an exception s[top] = x; top++; } 18 -14

Array Implementation To remove an item from the stack char pop() { if (is. Empty()) {throw(Stack. Is. Empty. Exception); }// throw an exception top--; return } s[top]; 18 -15

Array Implementation To look at the top item in the stack char peek() { if (is. Empty()) {throw(Stack. Is. Empty. Exception); }// throw an exception } return s[top]; 18 -16

Simple Uses of a Stack • Reverse a list of characters • Postfix calculator • Deck of cards!! • Program structure • …. Lots more Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Using Stacks with Algebraic Expressions • Evaluating postfix expressions The effect of a postfix calculator on a stack when evaluating the expression 2 * (3 + 4) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
- Slides: 19