5.4 Additional Stack Applications

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
COSC 2006 Chapter 7 Stacks III
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
Topic 3 The Stack ADT.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Computing with C# and the .NET Framework
Data Structure By Amee Trivedi.
Stacks Chapter 5.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Stacks Stacks.
Homework 4 questions???.
Stacks Chapter 7 introduces the stack data type.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack.
Chapter 5 Stacks.
Wednesday, February 28, 2018 Announcements… For Today…
Introduction to Data Structures
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.1 The Stack Abstract Data Type
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 6 – Queues and Deques
5.3 Implementing a Stack Chapter 5 – The Stack.
5.1 The Stack Abstract Data Type
LINEAR DATA STRUCTURES
CHAPTER 3: Collections—Stacks
Presentation transcript:

5.4 Additional Stack Applications 5.3 Implementing a Stack 5.4 Additional Stack Applications Chapter 5 – The Stack

Attendance Quiz #19 Stacks

Tip #20: Initialization Lists Stacks One aspect of C++ that isn't fickle is the order in which an object's data is initialized. The order is always the same: Base classes are initialized before derived classes. Within a class, data members are initialized in the order in which the are declared. To avoid reader confusion, as well as the possibility of some truly obscure behavioral bugs, always list members in the initialization list in the same order as they're declared in the class. class MyClass { private: string name; string address; public: MyClass(const string& n, const string& a) : address(a), name(n) { } }; class MyClass { private: string name; string address; public: MyClass(const string& n, const string& a) : name(n), address(a) { } }; 

Abstract Data Type Stacks An Abstract Data Type (ADT) is a mathematical model for a data type as seen from a users point of view. In contrast to data structures, which are concrete representations of data, as seen from the implementers point of view, no the users. ADT's support abstraction, encapsulation, and information hiding. A user implementation of an ADT has: private data hidden inside the class, a collection of public operations to manipulate the data, and additional private operations hidden inside the class. For example, a cell phone has buttons for various operations, but it is not necessary to understand how the phone works in order to use it!

Abstract Data Type Examples of common built-in ADTs: Stacks Examples of common built-in ADTs: boolean Values: true and false Operations: and, or, not, nand, etc. integer Values: Whole numbers between MIN and MAX values Operations: add, subtract, multiply, divide, etc. Examples of common user-defined ADTs: vector Values: Vector elements (ie., vector of X's,…) Operations: initialize, push_back, pop_back, at, erase, swap, size, etc. list Operations: initialize, front, back, size, merge, sort, etc. stack Values: Stack elements (ie., stack of X's,…) Operations: push, pop, top, empty size.

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. 325-331

The Stack ADT A stack is similar to a vector. Stacks A stack is similar to a vector. 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.)

deque<Item_Type> for vector<Item_Type> myStack.h Stacks deque<Item_Type> for vector<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. The stack class uses the delegation pattern by making the functions in the underlying container class do its work. 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.

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.

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).

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 linked data structure is under-utilized.

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. 332-347

Postfix and infix notation Stacks Postfix and infix notation Expressions normally are written in infix form, but it easier for a computer to evaluate an expression in postfix form since there is no need to group sub-expressions in parentheses or worry about operator precedence Save operands on a stack until their associated operator is scanned, then pop the operands, compute the result, and push it back onto the stack. Assume binary operators and disallow negative operands.

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 4

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7 4

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7 4

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 28

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 28

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 20 28

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 8

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 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 8

Evaluating Postfix Expressions Stacks Testing: write a driver which 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

Using the expression: [(43 + {2}) * (19)] Determinine if it is balanced. Convert to Postfix. Evaluate the expression.

Convert Infix to Postfix

Converting from Infix to Postfix Stacks Assume: expressions consists of only spaces, operands, and operators space is a delimiter character all operands that are identifiers begin with a letter all operands that are numbers begin with a digit For expressions without parentheses, there are two criteria that determine the order of operator evaluation: Operators are evaluated according to their precedence or rank; higher- precedence operators are evaluated before lower-precedence operators. For example, *, /, and % (the multiplicative operators) are evaluated before +, – Operators with the same precedence are evaluated in left-to-right order (left associative rule) Example convert: w – 5.1 / sum * 2 to its postfix form: w 5.1 sum / 2 * -

Converting from Infix to Postfix Stacks By temporarily storing the operators on a stack, we can pop them when needed and insert them in the postfix string in an order that indicates when they should be evaluated For example, if we have the first two operators from the string "w - 5.1 / sum * 2" stored on a stack as follows: The operator “/” (scanned second) must come off the stack and be placed in the postfix string before the operator - (scanned first) If we have the stack as just shown and the next operator is “*”, we need to pop the “/” off the stack and insert it in the postfix string before “*”, because the multiplicative operator scanned earlier (/) should be evaluated before the multiplicative operator (*) scanned later (the left- associative rule) / -

Converting from Infix to Postfix Stacks

Converting from Infix to Postfix Stacks

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( ( balanced : true index : 0

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: ( w 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( balanced : true index : 1

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( * ( balanced : true index : 2

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( [ * ( balanced : true index : 3

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w x 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( [ * ( balanced : true index : 4

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w x 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( + [ * ( balanced : true index : 5

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w x y 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( + [ * ( balanced : true index : 6

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: + w x y 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( ] + [ [ * ( balanced : true index : 7

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: * w x y + 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( / * ( balanced : true index : 8

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: w x y + * z 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( / ( balanced : true index : 9

Infix to Postfix Example Stacks Infix Expression: (w * [x + y] / z) Postfix Expression: * / w x y + * z 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( ) ( / ( balanced : true index : 10

For each of the following expressions 1) Check for balance 2) Convert to Postfix 3) Evaluate expression [ 3 + 3 ] ( 5 ) 40 * { 2 + 4 - ( 2 + 2 ) } - 4 / 5 / 6 [ 3 + 2 + 13 ] / 4 [ ( 3 + 4 ) * { 3 - 2 } - ( 2 / 2 ) ] + 24 / 3