Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks.

Slides:



Advertisements
Similar presentations
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Advertisements

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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 8 stack ( 堆疊 ) Speaker: Lung-Sheng Chien Reference book: Larry Nyhoff, C++ an introduction to data structures.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Object Oriented Data Structures
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
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.
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.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
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)
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
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.
1 8. Run-Time Arrays — Intro. to Pointers For declarations like double doubleVar; char charVar = 'A'; int intVar = 1234; the compiler constructs the object.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
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.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
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 
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.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Dr. Bernard Chen Ph.D. University of Central Arkansas
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Array Lists Chapter 6 Section 6.1 to 6.3
Pointers & Dynamic Data Structures
Jordi Cortadella and Jordi Petit Department of Computer Science
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks Chapter 7

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Contents 7.1 Introduction to Stacks 7.2 Designing and Building a Stack Class – Array-Based 7.3 Linked Stacks 7.4 Use of Stacks in Function Calls 7.5 Case Study: Postfix (RPN) Notation

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Chapter Objectives Study a stack as an ADT Build a static-array-based implementation of stacks Build a linked-implementation of stacks Show how a run-time stack is used to store information during function calls Study postfix notation and see how stacks are used to convert expressions from infix to postfix and how to evaluate postfix expressions

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction to Stacks Consider a card game with a discard pile –Discards always placed on the top of the pile –Players may retrieve a card only from the top We seek a way to represent and manipulate this in a computer program This is a stack What other examples can you think of that are modeled by a stack?

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction to Stacks A stack is a last-in-first-out (LIFO) data structure Adding an item –Referred to as pushing it onto the stack Removing an item –Referred to as popping it from the stack

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved A Stack Definition: –An ordered collection of data items –Can be accessed at only one end (the top) Operations: –construct a stack (usually empty) –check if it is empty –Push: add an element to the top –Top: retrieve the top element –Pop:remove the top element

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Example Program Consider a program to do base conversion of a number (ten to two) Note program which assumes existence of a Stack class to accomplish this, Fig 7.2Fig 7.2 –Demonstrates push, pop, and top

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved #include using namespace std; #include "Stack.h" // our own -- for STL version int main() { unsigned number, // the number to be converted remainder; // remainder when number is divided by 2 Stack stackOfRemainders; // stack of remainders char response; // user response do { cout << "Enter positive integer to convert: "; cin >> number; while (number != 0) { remainder = number % 2; stackOfRemainders.push(remainder); number /= 2; } cout << "Base-two representation: "; while ( !stackOfRemainders.empty() ) { remainder = stackOfRemainders.top(); stackOfRemainders.pop(); cout << remainder; } cout << endl; cout << "\nMore (Y or N)? "; cin >> response; } while (response == 'Y' || response == 'y'); }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Selecting Storage Structure Model with an array –Let position 0 be top of stack Problem … consider pushing and popping –Requires much shifting

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Selecting Storage Structure A better approach is to let position 0 be the bottom of the stack Thus our design will include –An array to hold the stack elements –An integer to indicate the top of the stack Note beginning of Stack.h file, Fig. 7.3 Fig. 7.3 Note beginning of Stack.h file, Fig. 7.3 Fig. 7.3

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Implementing Operations Constructor –Compiler will handle allocation of memory Empty –Check if value of myTop == -1 Push (if myArray not full) –Increment myTop by 1 –Store value in myArray [myTop] Top –If stack not empty, return myArray[myTop] Pop –If array not empty, decrement myTop Output routine added for testing

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved The Stack Class The completed Stack.h file, Fig. 7.4AFig. 7.4A –All functions defined –Note use of typedef mechanism Implementation file, Stack.cpp, Fig 7.4BFig 7.4B Driver program to test the class, Fig 7.5Fig 7.5 –Creates stack of 4 elements –Demonstrates error checking for stack full, empty

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved #include #ifndef STACK #define STACK const int STACK_CAPACITY = 128; typedef int StackElement; class Stack { public: /***** Function Members *****/ Stack(); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); private: /***** Data Members *****/ StackElement myArray[STACK_CAPACITY]; int myTop; }; // end of class declaration #endif

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved //--- Definition of Stack constructor Stack::Stack() : myTop(-1) { } //--- Definition of empty() bool Stack::empty() const { return ( ? ); }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved //--- Definition of push() void Stack::push(const StackElement & value) { if (myTop < STACK_CAPACITY - 1) { ? } else { cerr << "*** Stack full -- can't add new value ***\n" "Must increase value of STACK_CAPACITY in Stack.h\n"; exit(1); }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved //--- Definition of top() StackElement Stack::top() const { if ( !empty() ) return ( ? ); else { cerr << "*** Stack is empty " " -- returning garbage value ***\n"; return *(new StackElement); }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved //--- Definition of pop() void Stack::pop() { if ( !empty() ) { ? } else cerr << "*** Stack is empty -- " "can't remove a value ***\n"; }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Application of Stacks Consider events when a function begins execution Activation record (or stack frame) is created Stores the current environment for that function. Contents:

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Run-time Stack Functions may call other functions –interrupt their own execution Must store the activation records to be recovered –system then reset when first function resumes execution This algorithm must have LIFO behavior Structure used is the run-time stack

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved // --- # includes and prototypes of functions here int main() { int a = 3; f1(a); cout << endl; // A: Return here after f1 finishes } // --- Function f1 void f1(int x) { cout << f2(x+1); // B: Return here after f2 finishes} // --- Function f2 void f2(int p) { int q =f3(p/2) ; // C: Return here after f3 finishes return 2*q;} // --- Function f3 int f3(int n) { return n*n + 1; }

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Use of Run-time Stack When a function is called … Copy of activation record pushed onto run- time stack Arguments copied into parameter spaces Control transferred to starting address of body of function

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Use of Run-time Stack When function terminates Run-time stack popped –Removes activation record of terminated function –exposes activation record of previously executing function Activation record used to restore environment of interrupted function Interrupted function resumes execution