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
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. isEmpty: true if the stack currently contains no elements
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
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
Array Implementation Example This stack has max capacity 3, initially top = 0 and stack is empty. K E G K E E push('E'); push('K'); push('G'); top is 1 top is 2 top is 3
Stack Operations Example After three pops, top is 0 and the stack is empty E K E pop(); (remove G) pop(); (remove K) pop(); (remove E)
Stack Operations Example Push(3) top Push(4) Peek() Pop()
Class Implementation { private: char *s; int capacity, top; public: class STACK { private: char *s; int capacity, top; public: void push(char x); char pop(); char peek(); bool isEmpty(); STACK(int stackSize); STACK(); ~STACK() }; int StackIsEmpty = 1009; int StackFullException = 1010; See IntStack.h, IntStack.cpp, pr18-01.cpp
Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool isEmpty() { if (top == 0) return true; else return false; }
Array Implementation To add an item to the stack void push(char x) { if (top == STACK_SIZE()) {throw(StackFullException);} // throw an exception s[top] = x; top++; }
Array Implementation To remove an item from the stack char pop() { if (isEmpty()) {throw(StackIsEmptyException);}// throw an exception top--; return s[top]; }
Array Implementation To look at the top item in the stack char peek() { if (isEmpty()) {throw(StackIsEmptyException);}// throw an exception return s[top]; }
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