Presentation is loading. Please wait.

Presentation is loading. Please wait.

5.3 Implementing a Stack Chapter 5 – The Stack.

Similar presentations


Presentation on theme: "5.3 Implementing a Stack Chapter 5 – The Stack."— Presentation transcript:

1 5.3 Implementing a Stack Chapter 5 – The Stack

2 Attendance Quiz #15 Stacks

3 Attendance Quiz #16 Stacks

4 Tip #17: KISS is Better! What does the following code fragment do?
Stacks What does the following code fragment do? a ^= b; b ^= a; You're right! Swaps a and b. REALLY fast. Generic. No temp. BUT??? Is it worth the obfuscation? int temp = b; b = a; a = temp; More often than not, clarity and readability are better than cleverness and pretentious display of ability.

5 5.3, pgs. 325-331 5.3 Implementing a Stack
Adapter Classes and the Delegation Pattern Revisiting the Definition File stack.h Implementing a Stack as a Linked Data Structure Comparison of Stack Implementations 5.3, pgs

6 The Stack ADT A stack is similar to a restricted list.
Stacks A stack is similar to a restricted list. The standard library defines the stack as a template class that takes any of the sequential containers (vector, list, or deque. Implementations use sequential containers: A contiguous container (such as a vector), A linked container (such as special-purpose single-linked list.) Stack has-a container

7 vector<Item_Type> for stack<Item_Type>
Vector Stack Stacks vector<Item_Type> for stack<Item_Type> #ifndef MY_STACK_H_ #define MY_STACK_H_ #include <vector> template <typename T> class Stack { private: std::vector<T> myStack; public: /** Construct an empty stack */ Stack() {} /** Push item on stack */ void push(const T& item) { myStack.push_back(item); } /** Return top of stack */ T& top() { return myStack.back(); } /** Pop top of stack */ void pop() { myStack.pop_back(); } /** Return TRUE if stack is empty */ bool empty() const { return myStack.size() == 0; } /** Return number of stack entries */ size_t size() const { myStack.size(); } }; #endif // MY_STACK_H_ All sequential containers provide the functions empty, size, back (equivalent to stack::top), push_back (equivalent to stack::push) and pop_back (equivalent to stack::pop), so we could use any of these containers. A stack is an adapter class because it adapts the functions available in another class to the interface its clients expect by giving different names to essentially the same operations. The stack class uses the delegation pattern by making the functions in the underlying container class do its work..

8 Linked List Stack Stacks We can also implement a stack using a linked list of nodes push() inserts a node at the head of the list. top_of_stack = new Node(item, top_of_stack); top_of_stack->next references the old top of stack top() returns top_of_stack->data. pop() resets top_of_stack to the value stored in the next field of the list head and then deletes the old top of the stack (pointed to by old_top). When the stack is empty, top_of_stack == NULL.

9 list<Item_Type> for stack<Item_Type>
Linked List Stack Stacks list<Item_Type> for stack<Item_Type> #ifndef MY_STACK_H_ #define MY_STACK_H_ #include <list> template <typename T> class Stack { private: std::list<T> myStack; public: /** Construct an empty stack */ Stack() {} /** Push item on stack */ void push(const T& item) { myStack.push_back(item); } /** Return top of stack */ T& top() { return myStack.back(); } /** Pop top of stack */ void pop() { myStack.pop_back(); } /** Return TRUE if stack is empty */ bool empty() const { return myStack.empty(); } /** Return number of stack entries */ size_t size() const { myStack.size(); } }; #endif // MY_STACK_H_ A stack is an adapter class for linked lists because it adapts the functions available in another class to the interface its clients expect by giving different names to essentially the same operations.

10 Comparing Stack Implementations
Stacks The linked list implementation is similar to the implementation provided by the C++ standard library. By delegating the operations to an underlying sequential container, we avoid having to implement these operations ourselves. By using the functions push_back, pop_back, and back, we are assured of constant time (O(1)) performance, since the C++ standard requires that implementations of the sequential containers provide these operations in (amortized) constant time. Making a copy of the stacked item (or reference) is not considered – probably a wash. All stack operations using a sequential container (such as a vector, linked list, or deque) are O(1).

11 Comparing Stack Implementations
Stacks Use of the standard containers has a space penalty. To achieve the amortized constant performance for push_back, and so forth, the vector and deque allocate additional space. The vector will allocate an array twice the current size whenever it needs to allocate additional space. (The deque also allocates additional space.) While using a linked data structure has the advantage of using exactly as much storage as is needed for the stack, you do need to allocate storage for the links. The standard list class is a double-linked list; thus each Node contains pointers to both a next and a previous entry (not needed for the stack.) Because all insertions and deletions are at one end, the flexibility provided by a double-linked data structure is under-utilized.

12 Stack Errors Stack underflow. Stack overflow. Stack error handling.
Stacks Stack underflow. Pop(), Top() Stack overflow. Push() Stack error handling. Try-catch throw correct errors Testing Equivalence Classes Test top, pop, push, empty for each: Empty stack Stack with one element Stack with some elements, probably call pop/push twice or more often Stack full Push N distinct elements, pop N times and ensure the same elements are retrieved in reverse order

13 5.4, pgs. 332-347 5.4 Additional Stack Applications
Case Study: Evaluating Postfix Expressions Case Study: Converting from Infix to Postfix Case Study: Part 2: Converting Expressions with Parentheses 5.4, pgs

14 Postfix and infix notation
Stacks Postfix and infix notation Expressions normally are written in infix form, but Easier for a computer to evaluate an expression in postfix form since No need to group sub-expressions in parentheses or Worry about operator precedence. Infix Expression Postfix Expression Value 4 * 7 4 7 * 28 4 * ( ) * 36 (4 * 7) 4 7 * 8 3 + ((4 * 7) / 2) * 2 / + 17 Evaluate postfix expressions by Saving operands on a stack until their associated operator is scanned, Pop the operands and compute the result, Push result back onto the stack.

15 Evaluating Postfix Expressions
Stacks 4 4 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 4

16 Evaluating Postfix Expressions
Stacks 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 7 4

17 Evaluating Postfix Expressions
Stacks * 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 7 4

18 Evaluating Postfix Expressions
Stacks 28 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result

19 Evaluating Postfix Expressions
Stacks 4 4 7 7 * 20 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 28

20 Evaluating Postfix Expressions
Stacks 4 4 7 7 * 20 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 28

21 Evaluating Postfix Expressions
Stacks - 4 4 7 7 * 20 - 20 28 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 20 28

22 Evaluating Postfix Expressions
Stacks 8 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 8

23 Evaluating Postfix Expressions
Stacks = 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit push the token on the stack 6. else if the token is an operator pop the right operand off the stack pop the left operand off the stack evaluate the operation push the result onto the stack 11. pop the stack and return the result 8

24 Evaluating Postfix Expressions
Stacks Test driver: creates a Postfix_Evaluator object reads one or more expressions and reports the result catches the Syntax_Error exception. exercises each path by using each operator exercises each path through eval by trying different orderings and multiple occurrences of operators Tests for syntax errors: an operator without any operands a single operand an extra operand an extra operator a variable name the empty string

25 Evaluate the following postfix expressions:
2 3 6 * + 9 – * 7 - % * * * = __________

26


Download ppt "5.3 Implementing a Stack Chapter 5 – The Stack."

Similar presentations


Ads by Google