5.1 The Stack Abstract Data Type

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

The unorganized person’s data structure
Stacks Chapter 11.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
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.
EC-211 DATA STRUCTURES LECTURE 5. 2 Stack Data Structure Stack of Books Stack of Notes Stack of Gifts.
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 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.
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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
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,
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.
Object Oriented Data Structures
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.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
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.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Data Structures & Algorithms
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture 7 : Intro. to STL (Standard Template Library)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
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 CSCI 132, Spring 2016 Notes_ 5 Stacks.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Mark Redekopp David Kempe
CSCE 210 Data Structures and Algorithms
Chapter 4 Stacks
Stacks Chapter 5.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Stacks Stacks.
Revised based on textbook author’s notes.
Homework 4 questions???.
Stacks Chapter 4.
Stack.
Chapter 5 Stacks.
Monday, February 26, 2018 Announcements… For Today…
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.1 The Stack Abstract Data Type
5.4 Additional Stack Applications
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
Introduction to C++ Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers & Dynamic Data Structures
Jordi Cortadella and Jordi Petit Department of Computer Science
COMS 261 Computer Science I
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
16 – Sequential Containers
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

5.1 The Stack Abstract Data Type 5.2 Stack Applications Chapter 5 – The Stack

Tip #16: Virtual Destructors Sequential Containers If a class has any virtual function, it should have a virtual destructor. Without base virtual destructor, calling delete on a base pointer leads to undefined behavior. class Base { public: Base() { cout << "Base()"; } virtual ~Base() { cout << "~Base()"; } }; class Derived : public Base Derived() { cout << "Derived()"; } ~Derived() { cout << "~Derived()"; } } int main() Base* b = new Derived(); delete b; return 0; Base() Derived() ~Derived() ~Base() class Base { public: Base() { cout << "Base()"; } ~Base() { cout << "~Base()"; } }; class Derived : public Base Derived() { cout << "Derived()"; } ~Derived() { cout << "~Derived()"; } } int main() Base* b = new Derived(); delete b; return 0; Base() Derived() ~Base() Derived1 Base Derived2

5.1, pgs. 312-315 5.1 The Stack Abstract Data Type Specification of the Stack Abstract Data Type 5.1, pgs. 312-315

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.

Stack Abstract Data Type Stacks A stack can be compared to a Pez dispenser Only the top item can be accessed You can extract only one item at a time The top element in the stack is the last added to the stack (most recently). The stack’s storage policy is Last-In, First-Out, or LIFO. Only the top element of a stack is visible; therefore the number of operations performed by a stack are few:

A Stack of Strings stack<string> names; names.push("Rich"); names.push("Debbie"); names.push("Robin"); names.push("Dustin"); names.push("Jonathan"); cout << names.empty(); string last = names.top(); names.pop(); names.push("Philip");

5.2 Stack Applications 5.2, pgs. 315-325 Case Study: Finding Palindromes Case Study: Testing Expressions for Balanced Parentheses 5.2, pgs. 315-325

Finding Palindromes l a m Stacks Palindrome: a string that reads identically in either direction, letter by letter (ignoring case and spaces) Bombard a drab mob Anne, I vote more cars race Rome to Vienna. Solution: If we scan the input string from left to right and push each character in the input string onto a stack of characters, we can form the reverse of the string by popping the characters and joining them together in the order that they come off the stack. For example: The stack at left contains the string “llama mall“. If we pop them off and join them together, we get "l" + "l" + "a" + "m" + "a" + "m" + "a" + "l" + "l", or the string "llama mall". When the stack is empty, we can compare the string we formed with the original. If they are the same, the original string is a palindrome. l a m

Palindrome Functor Push lower case palindrome characters onto stack. Stacks #include <string> #include <stack> using std::string; class Palindrome { private: string palindrome; public: Palindrome(const std::string& str) : palindrome(str) {} bool operator()() stack<char> myStack; string reverse; for (int i = 0; i < palindrome.size(); ++i) palindrome[i] = tolower(palindrome[i]); myStack.push(palindrome[i]); } while (!myStack.empty()) reverse += myStack.top(); myStack.pop(); return reverse == palindrome; }; #include <iostream> #include "palindrome.h" using namespace std; int main() { string palindrome = "kayaka"; Palindrome Palindrome(palindrome); cout << "\"" << palindrome << "\" is "; if (!Palindrome()) cout << "not "; cout << "a palindrome"; return 0; } Push lower case palindrome characters onto stack. Pop characters off stack and append in reverse order. Compare strings.

Testing Stacks Test the Palindrome_Finder class using the following inputs (equivalent classes): A single character (always a palindrome) Multiple characters in one word Multiple words Different cases Even-length strings Odd-length strings An empty string (considered a palindrome) Other Questions Null string? Phrase palindromes? Punctuation?

Balanced Expressions Stacks When analyzing arithmetic expressions, it is important to determine if an expression is balanced with respect to parentheses. The expression below is balanced: (w * (x + y) / z – (p / (r - q))) The problem is complicated if braces or brackets are used in conjunction with parentheses. The expression below is not balanced: (w * [x + y) / z – [p / {r – q}]) An expression is balanced if each subexpression that starts with the symbol “{” ends with the symbol “}”, and the same statement is true for the other symbol pairs.

Design Algorithm for Function is_balanced Stacks Algorithm for Function is_balanced 1. Create an empty stack of characters. 2. Assume that the expression is balanced (balanced is true). 3. Set index to 0. 4. while balanced is true and index < the expression’s length 5. Get the next character in the data string. 6. if the next character is an opening parenthesis 7. Push it onto the stack. 8. else if the next character is a closing parenthesis 9. Pop the top of the stack. 10. if stack was empty or top does not match the closing parenthesis 11. Set balanced to false. 12. Increment index. 13. Return true if balanced is true and the stack is empty.

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

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

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

Balanced Parentheses Example Stacks Expression: (w * [x + y] / z) 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( ] 5 7 6 [ [ ( Matches! Balanced still true balanced : true index : 4

Balanced Parentheses Example Stacks Expression: (w * [x + y] / z) 1 4 5 6 7 8 9 10 3 2 ( w x + y ] / z ) [ * ( ) 10 9 ( ( Matches! Balanced still true balanced : true index : 8

Implementation Let's use a vector for our stack. Stacks #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_ Let's use a vector for our stack. Delegate most functions to the vector stack.

Implementation Stacks /** Program to check an expression for balanced parentheses. */ #include <iostream> #include <string> #include <stack> using namespace std; const string OPEN = "([{"; // set of opening parentheses const string CLOSE = ")]}"; // corresponding closing parentheses. /** Function to determine if open parentheses */ bool is_open(char ch) { return OPEN.find(ch) != string::npos; } /** Function to determine if closing parentheses */ bool is_close(char ch) return CLOSE.find(ch) != string::npos; npos is a static member constant value with the greatest possible value for an element of type size_t.

Implementation Use iterator to process expression Stacks bool is_balanced(const string& expression) { stack<char> s; bool balanced = true; string::const_iterator iter = expression.begin(); while (balanced && (iter != expression.end())) ++iter; } return balanced && s.empty(); Use iterator to process expression char next_ch = *iter; if (is_open(next_ch)) s.push(next_ch); else { } Input: Reference to expression string Output: true if balance, else false if (is_close(next_ch)) { if (s.empty()) balanced = false; else char top_ch = s.top(); s.pop(); balanced = (OPEN.find(top_ch) == CLOSE.find(next_ch)); } Push opening parenthesis' on stack Compare top-of-stack with next_ch

Implementation /** Main function to test is_balanced. */ int main() { Stacks /** Main function to test is_balanced. */ int main() { cout << "Enter an expression" << endl; string expression; while (getline(cin, expression) && (expression != "")) cout << endl << expression; if (is_balanced(expression)) cout << " is balanced" << endl; } else cout << " is not balanced" << endl; cout << "Enter another expression: "; return 0;

Testing Stacks Use equivalent classes to provide a variety of input expressions displaying the result true or false. Try several levels of nested parentheses. Try nested parentheses where corresponding parentheses are not of the same type. Try unbalanced parentheses. Try no parentheses at all! PITFALL: If you attempt to pop an empty stack your program will probably halt and report a run-time error. The nature of the error message will depend on the compiler and operating system. It is possible that no error is reported at all. You can guard against this error by testing for a nonempty stack before calling top or pop.

[ ( 3 + 4 ) * [ { 3 - 2 } - ( 2 / 2 ) ] + 24 ] / 3 3 % { 7 - ( 2 / [ ( 5 – 81 ) ] } + 1 )