Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures.

Slides:



Advertisements
Similar presentations
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Advertisements

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.
Chapter 6 Queue. Learning Objectives ● Describe the behavior of a queue. ● Enumerate the primary operations supported by a queue. ● Learn about the UNIX.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
Topic 15 Implementing and Using Stacks
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
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 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
Data Structures and Algorithms
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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)
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CHP-3 STACKS.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
Chapter 6 Queue.
Data Structure By Amee Trivedi.
Stacks Chapter 5.
Recap: Solution of Last Lecture Activity
Objectives In this lesson, you will learn to: Define stacks
Cinda Heeren / Geoffrey Tien
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Chapter 7 Stack.
Chapter 7 Stack.
More About Stacks: Stack Applications
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Topic 15 Implementing and Using Stacks
Introduction to Stacks
Introduction to Stacks
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 7 Stack

Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures in computing.

Learning Objectives ● Describe the behavior of a stack. ● Enumerate the primary operations supported by the stack. ● Examine several applications of the stack, including parentheses matching, evaluating postfix expressions, and the conversion of an infix expression to postfix form. ● Understand the public interface of a stack class in Java and the running times of its methods.

Learning Objectives ● Develop a postfix package in Java to implement postfix expression evaluation. ● Study the implementation of a stack class in Java, and the trade-offs involved in choosing between candidate reusable components.

7.1 Stack Properties ● Surfing the Web on a browser:  The sequence of back clicks loads the browser with Web pages in reverse order of visit.  The last visited page is the first loaded when going back. ● A stack is a collection of entries that demonstrates exactly this last in, first out behavior, called LIFO in short.

7.1 Stack Properties

● An entry added or pushes on to the top of a stack. ● An entry is removed, or popped from the top of stack.

7.1 Stack Properties

Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View

Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View

Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View

Results of Stack Operations Using StackNode (Cont’d) Internal View Abstract View

Results of push Operation

Results of push Operation (Cont’d)

Results of pop Operation top = top.next

7.2.2 Postfix Expression Evaluation ● We write arithmetic expressions like so: ● Consider the expression: ● It cannot simply be scan left to right.

7.2.2 Postfix Expression Evaluation ● Postfix, does away with the need for parentheses. ● An operator always follows the operands or sub-expressions on which it operates.

7.2.2 Postfix Expression Evaluation

● Two conditions that must be met for the evaluation process to terminate successfully:  When an operator is encountered, there must exist a most recent pair of operands or temporary results for application.  When the scanning of the expression is complete, there must be exactly one value on the stack.

7.2.2 Postfix Expression Evaluation

● Two possible errors that may be encountered by the algorithm:  One is that of insufficient operands.  The other is that of too many operands. ● Insufficient operands case is detected when the token is an operator, but the stack has less than the two operands on which the operator must be applied. ● Too many operands case is detected after the while loop, when the stack has more than one entry in it.

7.3 A Stack Class

7.4 A Postfix Expression Evaluation Package ● Every step of the evaluation processes one token of the expression.

7.4.1 Class PostfixEvaluator

 java.util.StringTokenizer parses the postfix expression into tokens and deals them out one at a time.  StackKeeper maintains the evaluation stack.

7.4.1 Class PostfixEvaluator

● RunAll message evaluates and produces the results in one shot. ● Restart the evaluator by the init message.

7.4.3 Class StackKeeper

7.4.4 Class PostfixEvaluator Implementation

● The StringTokenizer method count Tokens returns the number of tokens that remain to be enumerated.  At the end of the run, the stack must contain exactly one element.

7.4.4 Class PostfixEvaluator Implementation ● The NoSuchElementException does two things:  Prints the current evaluation status so the calling application gets as much information as possible about the source of the exception.  Throws an IllegalExpressionException, in order to deliver the most precise and complete information about the cause of the exception. ● This is much better than just passing through the NoSuchElementException, which, in this context, is not informative enough.

7.5.1 Design1: Array List for Storage ● Using an instance of the ArrayList class as a component in the Stack class.

7.5.2 Design 2: Linked List for Storage ● Which end of the list should be used for the pushes and pops?  Addition could be done at either end on O(1) time.  Deletion from the front can be done in O(1) time, deletion from the rear can only be done in O(n) time.

7.5.2 Design 2: Linked List for Storage

● The insertAt (item, index) method takes O(n) time in the worst case, but insertAt (item, 0) only times O(1) time. ● The removeAt (index) method takes O(n) time in the worst case, but removeAt (0) only takes O(1) time.

7.6 Summary ● The stack data structure implements the Last In First Out (LIFO) abstraction. ● A stack is a linear collection of entires in which, for every entry y that enters the stack after another entry x, y leaves the stack before x. ● A stack memorizes things and recalls them in reverse order. ● There are two fundamental operations supported by a stack: push and pop.

7.6 Summary ● Every infix expression can be written unambiguously in postfix form. ● A postfix expression can be evaluated by using a stack with a single left-to-right scan of the expression. ● A stack class in Java may define more than just the fundamental push and pop methods in its interface in order to provide better efficiency and ease of use.

7.6 Summary ● A stack is a powerful tool for parentheses matching, or in general, matching entities that must occur in opening and closing pairs. ● An infix expression may be converted in O(n) (linear) time into the equivalent infix form, using a stack. ● A stack class may be implemented using a vector or a linked list as component, with careful attention being paid to the “end” at which push and pop are performed in order to maintain O(1) time for these operations.