Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.

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

Stacks Chapter 11.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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)
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CMPT 225 Stacks-part2.
CMPT 225 Stacks.
Chapter 3 Stacks.
Topic 15 Implementing and Using Stacks
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
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:
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.
Section 5.2 Defining Languages. © 2005 Pearson Addison-Wesley. All rights reserved5-2 Defining Languages A language –A set of strings of symbols –Examples:
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.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Transforming Infix to Postfix
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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 (and a bit of generics for flavor)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Recursion as a Problem-Solving Technique. What is recursion? There are two types of recursive functions: Direct and Indirect Explain… There are two basic.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CSE 3358 NOTE SET 8 Data Structures and Algorithms.
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.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
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.
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
COMPSCI 107 Computer Science Fundamentals
Abstract Data Types (ADTs)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Data Structures
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Chapters 6 and 7 Stacks.
Data Structures and Algorithms 2/2561
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Chapter 6 B Stacks

© 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately array based or reference based Fixed size versus dynamic size –An array-based implementation Uses fixed-sized arrays –Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached –A reference-based implementation Does not put a limit on the size of the stack

© 2004 Pearson Addison-Wesley. All rights reserved6 B-3 Comparing Implementations An implementation that uses a linked list versus one that uses a reference-based implementation of the ADT list –Linked list approach More efficient –ADT list approach Reuses an already implemented class –Much simpler to write –Saves time

© 2004 Pearson Addison-Wesley. All rights reserved6 B-4 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 expressions –Convert the infix expression to postfix form –Evaluate the postfix expression

© 2004 Pearson Addison-Wesley. All rights reserved6 B-5 Evaluating Postfix Expressions A postfix calculator –Requires you to enter postfix expressions Example: 2, 3, 4, +, * –When an operand is entered, the calculator Pushes it onto a stack –When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack

© 2004 Pearson Addison-Wesley. All rights reserved6 B-6 Evaluating Postfix Expressions Figure 6.7 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

© 2004 Pearson Addison-Wesley. All rights reserved6 B-7 Evaluating Postfix Expressions To evaluate a postfix expression which is entered as a string of characters –Simplifying assumptions The string is a syntactically correct postfix expression No unary operators are present No exponentiation operators are present Operands are single lowercase letters that represent integer values

© 2004 Pearson Addison-Wesley. All rights reserved6 B-8 for (each character ch in the string) if (ch is an operand) { Push value that operand ch represents onto stack } else { // ch is an operator named op // evaluate and push the result operand1 = Pop the top of the stack operand2 = Pop the top of the stack result = operand1 op operand2 Push result onto stack } // end if } // end for

© 2004 Pearson Addison-Wesley. All rights reserved6 B-9 Converting Infix Expressions to Equivalent Postfix Expressions An infix expression can be evaluated by first being converted into an equivalent postfix expression Facts about converting from infix to postfix –Operands always stay in the same order with respect to one another –An operator will move only “to the right” with respect to the operands –All parentheses are removed

© 2004 Pearson Addison-Wesley. All rights reserved6 B-10 1.When you encounter an operand, append it to the output string postfixExp. 2.Push each “(“ on to the stack 3.When you encounter an operator, if the stack is empty, push the operator onto the stack. However, if the stack is not empty, pop operators of greater or equal precedence from the stack and append them to postfixExp. You stop when you encounter either a “(“ or an operator of lower precedence or when the stack becomes empty. You the push the new operator onto the stack. 4.When you encounter a “)”, pop operators off the stack and append them to the end of postfixExp until you encounter the matching”(“. 5.When you reach the end of the string, you append the remaining contents of the stack to postfixExp.

© 2004 Pearson Addison-Wesley. All rights reserved6 B-11 Converting Infix Expressions to Equivalent Postfix Expressions Figure 6.8 A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form

© 2004 Pearson Addison-Wesley. All rights reserved6 B-12 for (each character ch in the infix expression) switch (ch) { case operands: // append operand to end of postfixExp postfixExp = postfixExp + ch break case ‘(‘: // save ‘(‘ on stack stack.push(ch) break case ‘)‘: // pop stack unitl matching ‘(‘ while (top of stack is not ‘(“) { postfixExp = postfixExp + stack.pop() } // end while openParen = stack.pop() // remove the open parenthesis break

© 2004 Pearson Addison-Wesley. All rights reserved6 B-13 case operator: // process stack operators of // greater precedence while (!stack.isEmpty() and top of stack is not ‘(“ and precedence(ch) <= precedence(top of stack) ) { postfixExp = postfixExp + stack.pop() } // end while stack.push(ch) // save new operator break } // end switch } // end for while (!stack.isEmpty()) { postfixExp = postfixExp + stack.pop() } // end while

© 2004 Pearson Addison-Wesley. All rights reserved6 B-14 Application: A Search Problem High Planes Airline Company (HPAir) –Problem For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city

© 2004 Pearson Addison-Wesley. All rights reserved6 B-15 Representing the Flight Data The flight map for HPAir is a graph –Adjacent vertices Two vertices that are joined by an edge –Directed path A sequence of directed edges Figure 6.9 Flight map for HPAir

© 2004 Pearson Addison-Wesley. All rights reserved6 B-16 A Nonrecursive Solution that Uses a Stack The solution performs an exhaustive search –Beginning at the origin city, the solution will try every possible sequence of flights until either It finds a sequence that gets to the destination city It determines that no such sequence exists The ADT stack is useful in organizing an exhaustive search Backtracking can be used to recover from a wrong choice of a city

© 2004 Pearson Addison-Wesley. All rights reserved6 B-17 stack.createStack() stack.push(originCity) // push origin city onto stack while (a sequence of flights from the origin to the destination has not been found) { if (you need to backtrack from the city on the top of the stack) { temp = stack.pop() } else { Select a destination city C for a flight from the city on the top of the stack stack.push(C) } // end if } // end while

© 2004 Pearson Addison-Wesley. All rights reserved6 B-18 A Nonrecursive Solution that Uses a Stack Figure 6.10 The stack of cities as you travel a) from P; b) to R; c) to X; d) back to R; e) back to P; f) to W

© 2004 Pearson Addison-Wesley. All rights reserved6 B-19 stack.createStack() Clear marks on all cities stack.push(originCity) // push origin city onto stack Mark the origin as visited while (a sequence of flights from the origin to the destination has not been found) { // loop invariant: The stack contains a directed graph // from the origin city at the bottom of the stack to // the city at the top of the stack if (no flights exist from the city on the top of the stack to unvisited cities) { temp = stack.pop() // backtrack } else { Select an unvisited destination city C for a flight from the city on the top of the stack stack.push(C) Mark C as visited } // end if } // end while

© 2004 Pearson Addison-Wesley. All rights reserved6 B-20 A Nonrecursive Solution that Uses a Stack Figure 6.12 A trace of the search algorithm, given the flight map in Figure 6-9

© 2004 Pearson Addison-Wesley. All rights reserved6 B-21 A Recursive Solution Possible outcomes of the recursive search strategy –You eventually reach the destination city and can conclude that it is possible to fly from the origin to the destination –You reach a city C from which there are no departing flights –You go around in circles

© 2004 Pearson Addison-Wesley. All rights reserved6 B-22 A Recursive Solution A refined recursive search strategy searchR(originCity, destinationCity) Mark originCity as visited if (originCity is destinationCity) { Terminate -- the destination is reached } else { for (each unvisited city C adjacent to originCity) { searchR(C, destinationCity) }

© 2004 Pearson Addison-Wesley. All rights reserved6 B-23 The Relationship Between Stacks and Recursion The ADT stack has a hidden presence in the concept of recursion Typically, stacks are used by compilers to implement recursive methods –During execution, each recursive call generates an activation record that is pushed onto a stack Stacks can be used to implement a nonrecursive version of a recursive algorithm

© 2004 Pearson Addison-Wesley. All rights reserved6 B-24 Summary ADT stack operations have a last-in, first-out (LIFO) behavior Algorithms that operate on algebraic expressions are an important application of stacks A stack can be used to determine whether a sequence of flights exists between two cities A strong relationship exists between recursion and stacks