Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 6: Stacks Data Abstraction & Problem Solving with C++

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
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.
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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CMPT 225 Stacks.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Lecture 6: Stacks 1. 2 The Abstract Data Type Specifications of an abstract data type for a particular problem –Can emerge during the design of the problem’s.
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.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Abstract Data Types (ADTs)
Data Abstraction & Problem Solving with C++
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
CC 215 Data Structures Queue ADT
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Stacks.
Stack.
Chapter 4 Linked Lists
Pointers and Linked Lists
Chapter 6 Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Abstract Data Types (ADTs)
Chapters 6 and 7 Stacks.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Developing an ADT During the Design of a Solution ADT stack operations –Create an empty stack –Destroy a stack –Determine whether a stack is empty –Add a new item to the stack –Remove the item that was added most recently –Retrieve the item that was added most recently

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Developing an ADT During the Design of a Solution Figure 6-1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO) property The last item placed on the stack will be the first item removed –Analogy A stack of dishes in a cafeteria

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Refining the Definition of the ADT Stack Operation Contract for the ADT Stack isEmpty():boolean {query} push(in newItem:StackItemType) throw StackException pop() throw StackException pop(out stackTop:StackItemType) throw StackException getTop(out stackTop:StackItemType) {query} throw StackException

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Checking for Balanced Braces, Parenthesis and Brackets A stack can be used to verify whether a program contains balanced braces –An example of balanced braces abc{defg[ijk](l{mn})op}qr –An example of unbalanced braces abc{def]}[gh{ij(kl}m) though you have the same number of open and closing types

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Checking for Balanced Braces Requirements for balanced braces –Each time you encounter a “}])”, it should match the previously encountered “{[(” –When you reach the end of the string, you have matched each

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver In-class exercise: Write the pseudocode for the balancing parenthesis problem 7

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recognizing Strings in a Language L = {w$w’ : w is a possibly empty string of characters other than $, w’ = reverse(w) } A solution using a stack –Traverse the first half of the string, pushing each character onto a stack –Once you reach the $, for each character in the second half of the string, match a popped character off the stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Implementations of the ADT Stack The ADT stack can be implemented using –An array –A linked list –The ADT list All three implementations use a StackException class to handle possible exceptions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver An Array-Based Implementation of the ADT Stack Private data fields –An array of items of type StackItemType –The index top to the top item Compiler-generated destructor and copy constructor Figure 6-5 An array-based implementation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver StackA.h */ #include "StackException.h" const int MAX_STACK = maximum-size-of-stack; typedef desired-type-of-stack-item StackItemType; / * ADT stack - Array-based implementation. */ class Stack {public: Stack(); bool isEmpty() const; void push(const StackItemType& newItem) ; void pop(); void pop(StackItemType& stackTop) ; void getTop(StackItemType& stackTop) const;

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver private: /** Array of stack items */ StackItemType items[MAX_STACK]; /** Index to top of stack */ int top; }; // end Stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver StackA.cpp */ #include "StackA.h" // Stack class specification file Stack::Stack() : top(-1) { } // end default constructor bool Stack::isEmpty() const { return top < 0; } // end isEmpty void Stack::push(const StackItemType& newItem) { // if stack has no more room for another item if (top >= MAX_STACK-1) cout << "StackException: stack full on push"; else { ++top; items[top] = newItem; } // end if } // end push

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::pop() { if (isEmpty()) cout << “StackException: stack empty on pop"; else --top; // stack is not empty; pop top } // end pop void Stack::pop(StackItemType& stackTop) { if (isEmpty()) cout << "StackException: stack empty on pop"; else { // stack is not empty; retrieve top stackTop = items[top]; --top; // pop top } // end if } // end pop file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::getTop(StackItemType& stackTop) const { if (isEmpty()) cout << "StackException: stack empty "; else // stack is not empty; retrieve top stackTop = items[top]; } // end getTop // End of implementation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver #include #include "StackA.h" using namespace std; int main() { StackItemType anItem; Stack aStack; cin >> anItem; // read an item aStack.push(anItem); // push it onto stack return 0; } // end main

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Pointer-Based Implementation of the ADT Stack A pointer-based implementation –Enables the stack to grow and shrink dynamically topPtr is a pointer to the head of a linked list of items A copy constructor and destructor must be supplied Figure 6-6 A pointer-based implementation

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Pointer implementation of a stack 18 StackP.h */ #include "StackException.h" typedef desired-type-of-stack-item StackItemType; /** ADT stack - Pointer-based implementation. */ class Stack {public: Stack(); Stack(const Stack& aStack); ~Stack(); bool isEmpty() const; void push(const StackItemType& newItem) throw(StackException); void pop() throw(StackException); void pop(StackItemType& stackTop) throw(StackException); void getTop(StackItemType& stackTop) const throw(StackException);

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver private: /** A node on the stack. */ struct StackNode { /** A data item on the stack. */ StackItemType item; /** Pointer to next node. */ StackNode *next; }; // end StackNode /** Pointer to first node in the stack. */ StackNode *topPtr; }; // end Stack // End of header file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver StackP.cpp */ #include // for NULL #include // for bad_alloc #include "StackP.h" // header file using namespace std; Stack::Stack() : topPtr(NULL) { } // end default constructor Stack::Stack(const Stack& aStack) { if (aStack.topPtr == NULL) topPtr = NULL; // original list is empty else

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver { // copy first node topPtr = new StackNode; topPtr->item = aStack.topPtr->item; // copy rest of list StackNode *newPtr = topPtr; // new list pointer for (StackNode *origPtr = aStack.topPtr->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new StackNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } // end for newPtr->next = NULL; } // end if } // end copy constructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Stack::~Stack() { // pop until stack is empty while (!isEmpty()) pop(); // Assertion: topPtr == NULL } // end destructor bool Stack::isEmpty() const { return topPtr == NULL; } // end isEmpty

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::push(const StackItemType& newItem) throw(StackException) { // create a new node try { StackNode *newPtr = new StackNode; // set data portion of new node newPtr->item = newItem; // insert the new node newPtr->next = topPtr; topPtr = newPtr; } catch (bad_alloc e) { throw StackException( "StackException: push cannot allocate memory."); } // try } // end push

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::pop() throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty "); else { // stack is not empty; delete top StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; } // end if } // end pop

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::pop(StackItemType& stackTop) throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty "); else { // stack is not empty; retrieve and delete top stackTop = topPtr->item; StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; } // end if } // end pop

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver void Stack::getTop(StackItemType& stackTop) const throw(StackException) { if (isEmpty()) throw StackException("StackException: stack empty "); else // stack is not empty; retrieve top stackTop = topPtr->item; } // end getTop // End of implementation file.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Comparing Implementations Fixed size versus dynamic size –A statically allocated array-based implementation Fixed-size stack that can get full Prevents the push operation from adding an item to the stack, if the array is full –A dynamically allocated array-based implementation or a pointer-based implementation No size restriction on the stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Comparing Implementations A pointer-based implementation vs. one that uses a pointer-based implementation of the ADT list –Pointer-based implementation is more efficient –ADT list approach reuses an already implemented class Much simpler to write Saves programming time

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The STL Class stack Provides a size function Has two data type parameters –T, the data type for the stack items –Container, the container class that the STL uses in its implementation of the stack Uses the keyword explicit in the constructor’s declaration to prevent use of the assignment operator to invoke the constructor

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Application: Algebraic Expressions When the ADT stack is used to solve a problem, the use of the ADT’s operations should not depend on its implementation To evaluate an infix expression –Convert the infix expression to postfix form –Evaluate the postfix expression

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Evaluating Postfix Expressions A postfix calculator –When an operand is entered, the calculator Pushes it onto a stack –When an operator is entered, the calculator Pops the stack twice Applies the operation to the values popped Pushes the result of the operation onto the stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The STL stack Container The STL stack container may be implemented as a vector, a list, or a deque (which you will learn about later in this chapter). Because the stack container is used to adapt these other containers, it is often referred to as a container adapter.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The STL stack Container Here are examples of how to declare a stack of int s, implemented as a vector, a list, and a deque. stack > iStack;//Vector stack stack > iStack;// List stack stack iStack;// Default – deque stack

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The STL stack Container See the STL documentation that describes many of the stack container’s member functions.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver // This program demonstrates the STL stack // container adapter. #include #include #include using namespace std; void main(void) { int x; stack > iStack; for (x = 2; x < 8; x += 2) { cout << "Pushing " << x << endl; iStack.push(x); }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver cout << "The size of the stack is "; cout << iStack.size() << endl; for (x = 2; x < 8; x += 2) { cout << "Popping " << iStack.top() << endl; iStack.pop(); } } Program Output Pushing 2 Pushing 4 Pushing 6 The size of the stack is 3 Popping 6 Popping 4 Popping 2