Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
© 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,
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 Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 12 Introduction to Collections - Stacks
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
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.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
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.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 3: Collections Java Software Structures: Designing and Using.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
11/24/ CSC Outline  Stacks  Basic operations  Examples of use  Queues  Basic operations  Examples of use  Implementations  Array-based.
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)
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Linked Structures, LinkedStack
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Click to edit Master text styles Stacks Data Structure.
Collections & Stacks Collections A collection is an object that holds and organizes other objects It provides operations for accessing and managing.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stack: a Linked Implementation
Data Structures Using C++ 2E
CHAPTER 4: Linked Structures
Stacks Chapter 5.
Storage Strategies: Dynamic Linking
Stacks.
Chapter 14: Stacks.
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 5 Adapted from Pearson Education, Inc.
Java Software Structures: John Lewis & Joseph Chase
CSC 1052 Stacks 1/11/2019.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stack Implementations
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 6 Stacks

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract data type Demonstrate how a stack can be used to solve problems Examine various stack implementations Compare stack implementations

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-3 Stacks A stack is a linear collection whose elements are added and removed from one end A stack is LIFO – last in, first out The last element to be put on the stack is the first element to be removed A stack is usually depicted vertically, with additions and deletions occurring at the top of the stack

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-4 figure 6.1 A conceptual view of a stack

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-5 figure 6.2 The operations on a stack See StackADT.java (page 148)StackADT.java

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-6 figure 6.3 The StackADT interface in UML

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-7 Using Stacks Stacks are particularly helpful when solving certain types of problems Consider the undo operation in an application keeps track of the most recent operations in reverse order

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-8 Postfix Expressions Let's examine a program that uses a stack to evaluate postfix expressions In a postfix expression, the operator comes after its two operands We generally use infix notation, with parentheses to force precedence: (3 + 4) * 2 In postfix notation, this would be written *

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-9 Postfix Expressions To evaluate a postfix expression: scan from left to right, determining if the next token is an operator or operand if it is an operand, push it on the stack if it is an operator, pop the stack twice to get the two operands, perform the operation, and push the result onto the stack At the end, there will be one value on the stack, which is the value of the expression

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-10 figure 6.4 Using a stack to evaluate a postfix expression

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-11 Postfix Expressions To simplify the example, let's assume the operands to the expressions are integer literals Our solution uses an ArrayStack, though any implementation of a stack would suffice See Postfix.java (page 152)Postfix.java See PostfixEvaluator.java (page 153)PostfixEvaluator.java

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-12 figure 6.5 A UML class diagram for the postfix expression program

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-13 The LinkedStack Class Now let's examine a linked implementation of a stack We will reuse the LinearNode class that we used in Chapter 3 to define the linked implementation of a bag collection Internally, a stack is represented as a linked list of nodes, with a reference to the top of the stack and an integer count of the number of nodes in the stack

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-14 figure 6.6 A linked implementation of a stack

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-15 The push Operation // // Adds the specified element to the top of the stack. // public void push (Object element) { LinearNode temp = new LinearNode (element); temp.setNext(top); top = temp; count++; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-16 figure 6.7 The stack after pushing element E

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-17 The pop Operation // // Removes the element at the top of the stack and returns a // reference to it. Throws an EmptyStackException if the stack // is empty. // public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); Object result = top.getElement(); top = top.getNext(); count--; return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-18 figure 6.8 The stack after a pop operation

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-19 The ArrayStack Class Now let's examine an array-based implementation of a stack We'll make the following design decisions: maintain an array of Object references the bottom of the stack is at index 0 the elements of the stack are in order and contiguous an integer variable top stores the index of the next available slot in the array This approach allows the stack to grow and shrink at the higher indexes

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-20 figure 6.9 An array implementation of a stack

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-21 The push Operation // // Adds the specified element to the top of the stack, expanding // the capacity of the stack array if necessary. // public void push (Object element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-22 figure 6.10 The stack after pushing element E

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-23 The pop Operation // // Removes the element at the top of the stack and returns a // reference to it. Throws an EmptyStackException if the stack // is empty. // public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); top--; Object result = stack[top]; stack[top] = null; return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-24 figure 6.11 The stack after popping the top element

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-25 The java.util.Stack Class The Java Collections framework defines a Stack class with similar operations It is derived from the Vector class and therefore has some characteristics that are not appropriate for a pure stack The java.util.Stack class has been around since the original version of Java, and has been retrofitted to meld with the Collections framework

figure 6.12 A UML description of the java.util.Stack class

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-27 Analysis of Stack Operations Because stack operations all work on one end of the collection, they are generally efficient The push and pop operations, for both linked and array implementations, are O(1) Likewise, the other operations for all implementations are O(1) We'll see that other collections (which don't have that characteristic) aren't as efficient for all operations