Stacks.

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

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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 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 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.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
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.
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.
Stacks. 2 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.
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
Topic 15 Implementing and Using Stacks
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
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.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
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.
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,
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
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.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Click to edit Master text styles Stacks Data Structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
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
Stacks Chapter 5.
Revised based on textbook author’s notes.
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
Introduction to Data Structures
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Topic 15 Implementing and Using Stacks
Stack.
More About Stacks: Stack Applications
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Stacks

Lists are great, but… Lists are simply collections of items Useful, but nice to have some meaning to attach to them Restrict operations to create useful data structures We want to have ADTs that actually do something useful Example (from text): collecting characters on a line of text Example: doing math with operator precedence (more on this later) Example: matching braces Both of these applications can use a stack A stack is also an ADT! Stacks can be based on (abstract) lists! CMPS 12B, UC Santa Cruz Stacks

What is a stack? A stack is a data structure that keeps objects in Last-In-First-Out (LIFO) order Objects are added to the top of the stack Only the top of the stack can be accessed Visualize this like a stack of paper (or plates) Example: track characters entered on a command line What methods does a stack need? j a v a x c c x a v a j CMPS 12B, UC Santa Cruz Stacks

What methods are needed for a stack? Create a stack Determine whether a stack is empty (or how many items are on it) Add an object to the top of the stack (push) Remove an object from the top of the stack (pop) Does this return the object removed? Remove all of the objects from the stack Can be done by repeatedly calling pop until the stack is empty Retrieve the object from the top of the stack (peek) CMPS 12B, UC Santa Cruz Stacks

Stack example: matching braces and parens Goal: make sure left and right braces and parentheses match This can’t be solved with simple counting { (x) } is OK, but { (x} ) isn’t Rule: { ok string } is OK Rule: ( ok string ) is OK Use a stack Place left braces and parentheses on stack When a right brace / paren is read, pop the left off stack If none there, report an error (no match) CMPS 12B, UC Santa Cruz Stacks

Stack example: postfix notation HP calculators use postfix notation (as do some human languages) Operations are done by specifying operands, then the operator Example: 2 3 4 + * results in 14 Calculates 2 * (3 + 4) We can implement this with a stack When we see a operand (number), push it on the stack When we see an operator Pop the appropriate number of operands off the stack Do the calcuation Push the result back onto the stack At the end, the stack should have the (one) result of the calculation CMPS 12B, UC Santa Cruz Stacks

More on postfix notation Calculate 5 * (4 + 3) Numbers orderer 5 4 3 Operands ordered + * Note reverse order! Must compute + first! See example at right 5 7 * 4 3 + 5 4 3 + * 3 7 4 35 5 CMPS 12B, UC Santa Cruz Stacks

Postfix is nice, but infix is more common Postfix works if you’re used to HP calculators Most people are more used to infix Example: (8*4) + 5 Can we convert infix to postfix? Yes! Use a stack to do this… Observations Operands stay in the same order from infix to postfix Operator x moves “to the right” to ensure that x precedes any operands that it should CMPS 12B, UC Santa Cruz Stacks

How is this done? Use two stacks Rules are One for operators being reordered One for the actual postfix operations Rules are Operands always pushed onto the postfix stack “(“ pushed onto reorder stack For each operator Pop off reorder stack and push onto postfix stack until empty or “(“ or lower precedence operator Push operator onto postfix stack On “)”, pop off reorder stack until “(“ is found Delete “(“: postfix needs no parentheses At end of string, pop all off reorder stack and onto postfix stack CMPS 12B, UC Santa Cruz Stacks

Example reordering: a-(b+c*d)/e Operands always pushed onto the postfix stack “(“ pushed onto reorder stack For each operator Pop off reorder stack and push onto postfix stack until empty or “(“ or lower precedence operator Push operator onto postfix stack On “)”, pop off reorder stack until “(“ is found Delete “(“: postfix needs no parentheses At end of string, pop all off reorder stack and onto postfix stack Here, do operations rather than push operators onto postfix stack * d + e c c*d ( / b+c*d b (b+c*d)/e - a a-(b+c*d)/e Reorder stack Postfix stack CMPS 12B, UC Santa Cruz Stacks

Using interfaces to declare a stack Java has good support for abstract data types An interface is a Java class without any methods Classes may implement interfaces Example: StackInterface May be implemented by array, linked list, etc. We’ll go over implementation on Friday For now, useful to see how to declare functions using interfaces CMPS 12B, UC Santa Cruz Stacks

Interfaces and ADTs public interface StackADT { public int length (); public void popAll (); public void push (Object o); public Object pop () throws StackException; public Object peek () throws StackException; } public class StackException extends RunTimeException { … } public class StackArray { final int MAX_STACk = 50; private Object items[]; private int top; public StackArray () { // constructor } public int length () { return (top+1); } … } CMPS 12B, UC Santa Cruz Stacks

Assignment #2 Goal: count words in files using linked lists Run asgn2.pl for sample output You may need to do perl asgn2.pl file1 file2 Break the assignment down into small pieces Do this before writing lots of pseudocode Pieces may include Doubly linked lists (keep the list sorted) Breaking a line up into words Printing the results Parsing the command line You may not use code from elsewhere for Doubly linked lists CMPS 12B, UC Santa Cruz Stacks

Hints for Assignment #2 Start early! This program is (considerably) more difficult than the first You should have a good design by the end of this week Break the program into manageable pieces It looks really big unless you turn it into lots of short methods It’s relatively easy to write a 10–15 line method It’s hard to write a 100–150 line program Get doubly-linked lists working first Try things out with just one list, rather than one per letter of the alphabet Get word breakup working early Use short files for testing Any files with words, punctuation will work Try using Java source files CMPS 12B, UC Santa Cruz Stacks