5. Stacks and Queues.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Sit-in lab 4.  Given a number of strings containing only “a”, “b”, “c”, “d”  Check each string that it follows the pattern a n b m c m d n where m,
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Topic 15 Implementing and Using Stacks
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.
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,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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,
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.
30-Jun-15 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.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Building Java Programs
The Stack and Queue Types Lecture 10 Hartmut Kaiser
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CS2852 Week 5, Class 2 Today Queue Applications Circular Queue Implementation Testing SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
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.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Click to edit Master text styles Stacks Data Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Stacks. Stack ADT where we can only work with "top" – Top() : get value on top – Pop() : remove value on top – Push(value) : put new value on top.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
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.
Prefix notation in action
The eclipse IDE IDE = “Integrated Development Environment”
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.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Homework 4 questions???.
Stacks and Queues.
Stacks and Queues.
Building Java Programs
Principles of Computing – UFCFA3-30-1
CSC 205 – Java Programming II
Stacks and Queues.
Stacks.
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
slides created by Marty Stepp
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
Lab4 problems More about templates Some STL
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 9: Stack and Queue
Presentation transcript:

5. Stacks and Queues

Connection to LList Can be implemented as a specialized linked-list Stacks are LIFO (Last-in-first-out) Adding is called pushing – only happens at end Removing is called popping – also happens at end Queues are FIFO (First-in-first-out) Adding happens at the start Removing happens only at the end.

Implementation Strategy #1 Inheritance (“Is-A” relationship) Use extends to inherit from LinkedList Add a redundant push and pop method. - (in Java) can’t hide inherited methods (like insert) + not much work needed. This is the approach taken by: java.util.Queue java.util.Stack public class Stack<E> extends LinkedList<E> { public Stack() { super(); } public void push(E val) { add(val); } public E pop() { remove(size() – 1); } }

Implementation Strategy #2 Composition (“Has-A” relationship) Make a new Stack class, add a protected Llist member. + (in Java) we can hide the methods we don’t want to expose - (in Java) more work: we must write wrappers for everything we do want to expose public class Stack<E> { protected LinkedList<E> mList; public Stack() { mList = new LinkedList<E>(); } public void push(E val) { mList.add(val); } public E pop() { mList.remove(size() – 1); } // … every other method we wish to expose }

Our Lab One of the first steps in a compiler / interpreter is a syntax check. One important syntax check is brace-matching. Take a language like Lisp (this is a @*&-ized version, btw): (exec (= i 0) (= n (input "max: ")) (while (i < n) (exec (if (and (== 0 (% i 3)) (== 0 (% i 5))) (print "FizzBuzz") (if (== 0 (% i 3)) (print "Fizz") (if (== 0 (% i 5)) (print "Buzz") (print i) ) (= i (+ i 1)) ))

Our Lab, cont. Do all the parentheses match? If not, where does the mis-match happen (line & col#)? Note: if we color-code the parens… They have to match from inside-out (exec (= i 0) (= n (input "max: ")) (while (i < n) (exec (if (and (== 0 (% i 3)) (== 0 (% i 5))) (print "FizzBuzz") (if (== 0 (% i 3)) (print "Fizz") (if (== 0 (% i 5)) (print "Buzz") (print i) ) (= i (+ i 1)) ))

Our Lab, cont. This is elegantly solved by a stack …of “Tokens”, where a Token is The character The line it appeared on The position within the line it appeared on Read the file character-by-character If it’s an opening brace: “(“, “{“ or “[“ Create a new token and push it on the stack If it’s a closing brace: “)”, “}” or “]” Look at the top of the stack (peek). If this char matches the one on top (e.g. “{“ matches “}”): pop the top off the stack. If not, we have a syntax error. If we reach the end of the file, but the stack is non-empty, we have one or more syntax.