1 4. Stacks Introduction Consider the 4 problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

The unorganized person’s data structure
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Stacks Example: Stack of plates in cafeteria.
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.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Topic 15 Implementing and Using Stacks
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Infix, Postfix, Prefix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Chapter 8 stack ( 堆疊 ) Speaker: Lung-Sheng Chien Reference book: Larry Nyhoff, C++ an introduction to data structures.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stack Applications.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks CHAPTER 7 MAY 21, 2015 Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005.
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)
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.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
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.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks Stacks.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks Chapter 7 introduces the stack data type.
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
Stack Data Structure, Reverse Polish Notation, Homework 7
Stacks Chapter 5 Adapted from Pearson Education, Inc.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Data Structures – Week #3
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker (4) Calculate and display base- two representation 26 = ??????? 2

2 In the last problem: Remainders are generated in right-to-left order. We need to "stack" them up, then print them out from top to bottom. In these problems we need a "last-discarded-first-removed," "last-pushed-onto-first-removed," "last-stored-first-removed, " "last-generated-first-displayed" structured data type. In summary... a _____________________________structure.

3 Stack as an ADT A stack is: Basic operations 1. _________________________________ 2. Check if stack _______________ 3. _____________an element ______________ of the stack 4. _____________ the _____________ of the stack 5. _____________ the _____________ of the stack Terminology is from spring-loaded stack of plates in a cafeteria: Adding a plate pushes those below it down in the stack Removing a plate pops those below it up one position.

4 Example use of Stack BASE-CONVERSION ALGORITHM (See p ) /* This algorithm displays the base-2 representation of a base-10 number. Receive:a positive integer number Output:the base-two representation of number */ 1. Create an empty stack to hold the remainders. 2. While number ≠ 0: a. Calculate the remainder that results when number is divided by 2. b. Push remainder onto the stack of remainders. c. Replace number by the integer quotient of number divided by 2.

5 3. While the stack of remainders is not empty: a. Retrieve and remove the remainder from the top of the stack of remainders. b. Display remainder.

6 /* Program that uses a stack to convert the base-ten * representation of a positive integer to base two. * * Input: A positive integer * Output: Base-two representation of the number ***************************************************/ __________________ // our own -- for STL version #include using namespace std; int main() { unsigned number, // the number to be converted remainder; // remainder when number is // divided by 2 _______________________________________________________ char response; // user response

7 do { cout > number; while (number != 0) { remainder = number % 2; ____________________________________ ; number /= 2; } cout << "Base-two representation: "; while ( _________________________________ ) { ____________________________________ ; cout << remainder; } cout > response; } while (response == 'Y' || response == 'y'); )

8 Building a Stack Class Two steps: 1. ____________the class; and 2. ____________the class. 1. Design: __________________________needed to manipulate "real-world" objects being modeled by class. Operations are described: _______________________________________________. In a manner independent of any specific representation of object (because we have no idea of what data members will be available).

9 Stack Operations: Construction: Initializes an empty stack. Empty operation: Determines if stack contains any values Push operation: Modifies a stack by adding a value to top of stack Top operation: Retrieves the value at the top of the stack Pop operation: Modifies a stack by removing the top value of the stack To help with debugging, add early on: Output: Displays all the elements stored in the stack.

10 2. Implementing a Stack Class Define data members: consider storage structure(s) Attempt #1: Use an array with the top of the stack at position 0. e.g., Push 75, Push 89, Push 64, Pop + features: ________________________________________________________ – features: ________________________________________________________ Push Push Push Pop

11 Implementing Stack Class — Refined Keep the bottom of stack at position 0. Maintain a "pointer" myTop to the top of the stack. Instead of modeling a stack of plates, model a stack of books (or a discard pile in a card game.) myTop = __ Note: _______________________________________

12 Provide: An ________ data member to hold the____________________. An ________ data member to indicate the ________________. Problems: We need an array declaration of the form ArrayElementType myArray[ARRAYCAPACITY]; — What type should be used? Solution (for now): Use the typedef mechanism: ______________________________ // put this before the class declaration — What about the capacity? ______________________________ // put this before the class declaration Now we can declare the array data member in the private section: _______________________________________ Stack 's Data Members

13 Notes: 1. typedef makes StackElement a synonym for int 2Want a stack of reals, or characters, or... ? Change the typedef typedef double StackElementType; or typedef char StackElementType; or... When class library is recompiled, array elements will be double or char or... 3.An alternative to using and changing a typedef: Write a ____________ to build a Stack class whose element type is left unspecified. The element type is __________as a special kind of _______________ at compile time. This is the approach used in the Standard Template Library (STL) and in most of the other C++ libraries.

14 4.The typedef and declaration of STACK_CAPACITY outside the class declaration makes them:  easy to find when they need changing  accessible throughout the class and in any file that #include s Stack.h  without qualification If we put typedef of StackElement and declaration of the constant STACK_CAPACITY in public section of the class declaration, they can be accessed outside the class, but require qualification: ____________StackElement ____________STACK_CAPACITY 5. If we were to make STACK_CAPACITY a data member, we would probably make it a _________ data member: _________ const int STACK_CAPACITY = 128; This makes it a property of the class usable by all class objects, but they do not have their own copies of STACK_CAPACITY.

15 //Stack.h /* Stack.h provides a Stack class. * * Basic operations: * Constructor: Constructs an empty stack * empty: Checks if a stack is empty * push: Modifies a stack by adding a value at the top * top: Accesses the top stack value; leaves stack * unchanged * pop: Modifies a stack by removing the value at the * top * display: Displays all the stack elements * Class Invariant: * 1. The stack elements (if any) are stored in positions * 0, 1,..., myTop of myArray. * <= myTop < STACK_CAPACITY */

16 #ifndef STACK #define STACK const int STACK_CAPACITY = 128; typedef int StackElement; class Stack { /***** Function Members *****/ public:... /***** Data Members *****/ private: }; // end of class declaration... #endif

17 Stack 's Function Members Constructor: class Stack { public: /*--- Constructor --- Precondition: A stack has been declared. Postcondition: Stack has been constructed as an empty stack. */ _______________________... } // end of class declaration Simple enough to inline? ___________________________________________

18 A declaration Stack s; will construct s as follows:

19 empty: ReceivesStack containing it as a function member (perhaps implicitly) Returns: True if stack is empty, false otherwise. Member function? _______ Const function? (Shouldn't alter data members)? _______ Simple enough to inline? ______ class Stack { public:... /* --- Is the Stack empty? --- * Returns: true if the Stack containing this * function is empty and false otherwise ***************************************************/ ______________________________... };// end of class declaration ____________________________________________ { return (myTop == -1); }

20 Test driver: #include #include using namespace std; #include "Stack.h" int main() { Stack s; cout << boolalpha << "s empty? " << s.empty() << endl; } Output: s empty? ___________________ or if boolalpha omitted or not implemented: s empty? ___________________

21 push: ReceivesStack containing it as a function member (perhaps implicitly) Value to be added to stack Returns: Modified stack (perhaps implicitly) Member function? _______ Const function? _______ Simple enough to inline? _______ Add to class declaration: /* --- Add a value to the stack --- * * Receive: A value to be added to a Stack * Postcondition: Value is added at top of stack * provided there's space * Output: "Stack full" message if no space *************************************************/ _____________________________________________________

22 Add to Stack.cpp : void Stack::push(_____________________________________) { _________________________________________ {//Preserve stack invariant } // or simply,_______________________ = value; else cerr << "*** Stack full -- can't add new value ***\n" "Must increase value of STACK_CAPACITY in Stack.h\n"; }

23 Add at bottom of driver: for (int i = 1; i <= 128; i++) s.push(i); cout << "Stack should now be full\n"; s.push(129); Output: s empty? true Stack should now be full *** Stack is full -- can't add new value *** Must increase value of STACK_CAPACITY in Stack.h

24 Output: So we can test our operations. Receives: Stack containing it as a function member (perhaps implicitly) an ostream Output:Contents of Stack, from the top down. Member function?______ Const function?_____ Simple enough to inline?______ Add to class declaration: /* --- Display values stored in the stack --- * * Receive: The ostream out * Output: Stack's contents, from top down, to out ***************************************************/ _______________________________________________ Add to Stack.cpp ______ void ________ display(ostream & out) { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; }

25 Modify driver: /* for (int i = 1; i <= 128; i++) s.push(i); cout << "Stack should now be full\n"; s.push(129); */ for (int i = 1; i <= 4; i++) s.push(2*i); cout << "Stack contents:\n"; s.display(cout); cout << "s empty? " << s.empty() << endl; Output: s empty? true Stack contents: s empty? false

26 Add to class declaration: /* --- Return value at top of the stack --- * Return: value at the top of nonempty Stack * Output: "Stack empty" message if stack empty ***************************************************/ _________________________________________ top: Receives: Stack containing it as a function member (perhaps implicitly) Returns:Value at the top of the stack Output:Error message if stack is empty Member function? _______ Const function? _______ Simple enough to inline?_______

27 Add to Stack.cpp : StackElement Stack::top() const { if (myTop >= 0) return (myArray[myTop]); else { cerr << "*** Stack is empty " " -- returning garbage ***\n"; return myArray[STACK_CAPACITY-1]; } Add to driver at bottom: cout << "Top value: " << s.top() << endl; Output: Stack contents: s empty? false Top value: 8

28 pop: ReceivesStack containing it as a function member (perhaps implicitly) Returns: Stack with top element removed (perhaps implicitly) Output:Error message if stack is empty. Member function?_____ Const function?______ Simple enough to inline?______ class Stack... /* --- Remove value at top of the stack --- * Postcondition: Top value of stack (if any) has * been removed. * Output: "Stack-empty" message if stack is empty. **************************************************/ ____________________________... }; // end of class declaration

29 _______ void _______ pop() _______ { if (myTop >= 0) // Preserve stack invariant myTop--; else cerr << "*** Stack is empty -- " "can't remove a value ***\n"; } Add to driver at bottom: while (!s.empty()) { cout << "Popping " << s.top() << endl; s.pop(); } cout << "s empty? " << s.empty() << endl; Output: Stack contents: s empty? false Top value: 8 Popping 8 Popping 6 Popping 4 Popping 2 s empty? true

30 Application of Stacks:Run-time Stack Whenever a function begins execution (i.e., is activated), an activation record (or stack frame) is created to store the current environment for that function. Its contents include: What kind of data structure should be used to store these so that they can be recovered and the system reset when the function resumes execution?

31 Problem:FunctionA can call FunctionB FunctionB can call FunctionC... When a function calls another function, it interrupts its own execution and needs to be able to resume its execution in the same state it was in when it was interrupted. When FunctionC finishes, control should return to FunctionB. When FunctionB finishes, control should return to FunctionA. So, the order of returns from a function is the reverse of function invocations; that is, ________behavior.  Use a _________to store the activation records. Since it is manipulated at run-time, it is called the ____________________

32 What happens when a function is called? 1. ________ copy of its activation record _______________________ 2.Copy its arguments into the parameter spaces 3.Transfer control to the address of the function's body The ______ activation record in the run-time stack is always that of the function ____________________. What happens when a function terminates? 1._________ activation record of terminated function from the run-time stack 2.Use new top activation record to ______________________ ___________________________________________________ execution of the interrupted function.

33 Trace run-time stack for the following:... int main() {... f2(...); f3(...); } void f1(...) {...} void f2(...) {... f1(...);...} void f3(...) {... f2(...);...} Examples int factorial(int n) { if (n < 2) return 1; else return n * factorial(n-1); } What happens to the run-time stack when the following statement executes? int answer = factorial(4); This pushing and popping of the run-time stack is the real overhead associated with function calls that inlining avoids by replacing the function call with the body of the function.

34 Application of Stacks: RPN 1. What is RPN (Reverse Polish Notation)? A notation for arithmetic expressions in which operators are written _____________ the operands. Expressions can be written without using ____________________. Developed by Polish logician, Jan Lukasiewics, in 1950's Infix notation: operators written _________ the operands Postfix " (RPN):operators written _________ the operands Prefix " : operators written _________ the operands Examples: INFIX RPN (POSTFIX)PREFIX A + B __________________ ____________________ A * B + C __________________ ____________________ A * (B + C) __________________ ____________________ A - (B - (C - D)) __________________ ____________________ A - B - C - D __________________ ____________________

35 Evaluating RPN Expressions "By hand" (Underlining technique): 1. Scan the expression from left to right to find an operator. 2. Locate ("underline") the last two preceding operands and combine them using this operator. 3. Repeat until the end of the expression is reached. Example: *  * 2 __ *  2 8 *  ___  2 __ *  *  2 7 ____ - *  * 

36 Stack Algorithm: (p.195) Receive: An RPN expression. Return: A stack whose top element is the value of RPN expression (unless an error occurred). 1. Initialize an empty stack. 2. Repeat the following until the end of the expression is encountered: a. Get next token (constant, variable, arithmetic operator) in the RPN expression. b. If token is an operand, push it onto the stack. If it is an operator, then: (i) Pop top two values from the stack. If stack does not contain two items, signal error due to a malformed RPN and terminate evaluation. (ii) Apply the operator to these two values. (iii) Push the resulting value back onto the stack. 3. When the end of expression encountered, its value is on top of the stack (and, in fact, must be the only value in the stack). Push TEMP# Generate code : LOAD operand 1 op operand 2 STORE TEMP# :

37 Sample RPN Evaluation (p. 196) Example: * Push 2 Push 3 Push 4 Read + Pop 4, Pop 3, ___________ Push 7 Push 5 Push 6 Read - Pop 6, Pop 5, ___________ Push -1 Read - Pop -1, Pop 7, __________ Push 8 Read * Pop 8, Pop 2, ___________ Push = = 8 2 * 8 = 16

38 Unary minus causes problems Example:   OR   Use a different symbol: 5 3 ~ ~

39 Converting Infix to RPN By hand: Represent infix expression as an expression tree: A * B + C A * (B + C) ((A + B) * C) / (D - E)

40 C AB D E * + / - Traverse the tree in Left-Right-Parent order (postorder) to get _________: C AB D E * + / - Traverse tree in Parent-Left-Right order (preorder) to get ____________: Traverse tree in Left-Parent-Right order (inorder) to get ___________ — must insert ()'s C AB D E * + / -

41 By hand: "Fully parenthesize-move-erase" method: 1. Fully parenthesize the expression. 2. Replace each right parenthesis by the corresponding operator. 3. Erase all left parentheses. Examples: A * B + C  _____________  _____________ A * (B + C)  _____________  _____________ Exercise: ((A + B) * C) / (D - E)

42 Stack Algorithm: (p.199) 1. Initialize an empty stack of operators. 2. While no error has occurred and end of infix expression not reached a. Get next token in the infix expression. b. If token is (i) a left parenthesis:Push it onto the stack. (ii) a right parenthesis:Pop and display stack elements until a left parenthesis is encountered, but do not display it. (Error if stack empty with no left parenthesis found.) (iii) an operator:If stack empty or token has higher priority than top stack element, push token onto stack. (Left parenthesis in stack has lower priority than operators) Otherwise, pop and display the top stack element; then repeat the comparison of token with new top stack item. (iv) an operand:Display it. 3. When end of infix expression reached, pop and display stack items until stack is empty.

43 Example: Push ( Output Display A _____________ Push + Display B _____________ Push * Display C _____________ Read ) Pop *, Display *, _____________ Pop +, Display +, Pop ( _____________ Push / Push ( Display D Push - Push ( Display E Push - Display F Read ) Pop -, Display -, Pop ( Read ) Pop -, Display -, Pop ( Pop /, Display / (A+B*C)/(D-(E-F)) ABC*+D ABC*+DE ABC*+DEF (A+B*C)/(D-(E-F)) ( - / ( - ( - / / ABC*+DEF- ABC*+DEF-- ABC*+DEF--/