Stacks and Queues. David Greenstein Monta Vista

Similar documents
csci 210: Data Structures Stacks and Queues

CS350: Data Structures Stacks

More Data Structures (Part 1) Stacks

Interfaces & Generics

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

CMSC 132: Object-Oriented Programming II. Stack and Queue

Queues Fall 2018 Margaret Reid-Miller

Queues. Stacks and Queues

Stack and Queue. Stack:

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Data Structures G5029

Stacks Fall 2018 Margaret Reid-Miller

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Lecture 2: Stacks and Queues

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Summer Final Exam Review Session August 5, 2009

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Basic Data Structures

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Basic Data Structures 1 / 24

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Arrays and ArrayLists. David Greenstein Monta Vista High School

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CS61BL Summer 2013 Midterm 2

Lecture 4 Stack and Queue

Stack Abstract Data Type

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Building Java Programs

ITI Introduction to Computing II

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CE204 Data Structures and Algorithms Part 2

Programming Abstractions

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CSC 321: Data Structures. Fall 2012

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

PA3 Design Specification

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Abstract Data Types. Abstract Data Types

Chapter 2. Stack & Queues. M hiwa ahmad aziz

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

1. Introduction. Lecture Content

Programming Abstractions

Name CPTR246 Spring '17 (100 total points) Exam 3

Stacks and Queues as Collections

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Stacks and Queues

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

CS 206 Introduction to Computer Science II

Lecture Data Structure Stack

Lecture 3 Linear Data Structures

CSC 222: Computer Programming II. Spring 2005

CS24 Week 4 Lecture 2

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

BlockingArrayQueue.java. Page 1

Notes on access restrictions

Lecture 3 Linear Data Structures

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

CSE 143 SAMPLE MIDTERM

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Data Structures And Algorithms

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

CSC 1052 Algorithms & Data Structures II: Queues

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

infix expressions (review)

Introduction to Computer Science II (ITI 1121) Final Examination


Stacks Goodrich, Tamassia, Goldwasser Stacks

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

CSC 321: Data Structures. Fall 2013

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Lecture 2: Implementing ADTs

ITI Introduction to Computing II

Plan for Week. What is a linear structure? Stacks Queues and Deques, Oh My!

What can we do with Coin dispenser?

CS 211 Programming Practicum Spring 2017

CS 307 Final Fall 2009

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

! Instan:a:ng a Group of Product objects. ! InstanEaEng a Group of Friend objects. ! CollecEons can be separated into two categories

Linked Structures. See Section 3.2 of the text.

Stacks, Queues (cont d)

Plan for Week. Linear structures: Stack, Queue. Friday: More examples of recursion and alternatives. Guide to using practice-it for APT-like problems

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

THE UNIVERSITY OF WESTERN AUSTRALIA

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Implementing Dynamic Data Structures

Transcription:

Stacks and Queues David Greenstein Monta Vista

Stack vs Queue Stacks and queues are used for temporary storage, but in different situations

Stacks are Used for handling nested structures: processing directories within directories evaluating expressions within expressions c 2 = a 2 + b 2 2abcos( C )

Stacks are Used for handling branching processes: traversing a branching tree structure planning a move in a chess game tracking the sequence of method calls in a Java program (frame)

Stack Functions push - adding object to the top of the stack pop - removing object from the top of the stack peek - look at object on top of the stack isempty - true if the stack contains no objects; false otherwise myelements[5] myelements[4] myelements[3] myelements[2] myelements[1] myelements[0] value3 value2 value1 value0 Stack pointer sp

Hardware Stack Frame A Java method call creates a stack frame. The frame contains all the necessary information to return (the state ). Pointers SP - Stack Pointer (in all stacks) BP - Base Pointer (in stack frames) Base Stack overflow means large (infinite) number of method calls. Top https://sites.google.com/site/illustratedcomputer/stackframe

Properties of a Stack In an efficient implementation, push, pop, and peek methods run in O(1) time. pop and peek are expected to throw a NoSuchElementException if the stack is empty. If we implement the stack using an ArrayList or LinkedList, we do not need to explicitly throw a NoSuchElementException (Ch 20). myelements[5] myelements[4] myelements[3] myelements[2] myelements[1] myelements[0] value3 value2 value1 value0 Stack pointer sp

Properties of a Stack A stack of objects holds references to objects. If necessary, a stack can hold multiple references to the same object, but be careful!!! Changing a mutable object on a stack changes them all. It is best to push a copy of the object onto the stack, especially mutable objects, to insure all stack objects retain their original value. myelements[5] myelements[4] myelements[3] myelements[2] myelements[1] myelements[0] value3 value2 value1 value0 Stack pointer sp

java.util.stack class Part of the Java Collections Framework (Chapter 20). Stack is a generic class. It works with objects of a specified type (e.g. Stack<String>). Based on the legacy Vector class, similar to ArrayList. Stack methods: push, pop, peek, isempty. Has other methods but do not use them! myelements[5] myelements[4] myelements[3] myelements[2] myelements[1] myelements[0] value3 value2 value1 value0 Stack pointer sp

Stack using ArrayList import java.util.arraylist; import java.util.list; public class ArrayStack<E> implements Stack<E> { private List<E> elements; public ArrayStack ( ) { elements = new ArrayList<E>( ); } } public boolean isempty ( ) { return elements.isempty ( ); } public void push (E obj) { elements.add (obj); } public E pop ( ) { return elements.remove (elements.size ( ) - 1); } public E peek ( ) { return elements.get (elements.size ( ) - 1); } Where is the top of an ArrayStack?

Queues are Used for Processing events or messages in order of their arrival System tasks Queueing print jobs Entering keystrokes Processing mouse clicks

Queue Functions add - adding object to the back of the queue (enqueue) remove - removing object from the front of the queue (dequeue) c peek - look at object at 2 front of the queue isempty - true if the queue contains no objects; false otherwise

Properties of a Queue In an efficient implementation, add, remove, and peek methods run in O(1) time. A queue of objects holds references to objects. Like a stack, a queue can hold multiple references to the same object. Like a stack, it is best to add a copy of the object onto the queue, especially mutable objects, to insure all queue objects retain their original value.

Ring Buffer An efficient implementation of a queue. Instead of shifting elements to the front, just keep track of front ( read ) and back ( write ) pointers. Actual Implementation Conceptual Model

java.util.queue interface boolean isempty () boolean add (E obj) E remove () E peek () A generic interface, part of the Java Collections Framework (Chapter 20) Implemented by other classes like java.util.linkedlist and java.util.priorityqueue

SimpleCalc Project Evaluates arithmetic expressions using stacks. Two stacks: operand stack and operator stack. 4 + 9 * 7 5 * ( 3 + 2 ) 7 9 * 4 + Operand Stack Operator Stack Operand Stack Operator Stack

Questions?