Lecture 2: Stacks and Queues

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
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.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
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.
Building Java Programs
18 Chapter Stacks and Queues
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
Chapter 12: Data Structures
CSE 373: Data Structures and Algorithms
Stacks and Queues.
CSE 373: Data Structures and Algorithms
Stack and Queue APURBO DATTA.
Data Structures and Algorithms
Lecture 3: Working with Data Structures
Stacks and Queues.
Building Java Programs
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Lecture 2: Implementing ADTs
Lecture 2: Implementing ADTs
Data Structures and Algorithms
Stacks and Queues.
Lecture 3: Queues, Testing, and Working with Code
Lecture 26: Advanced List Implementation
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
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.
slides created by Marty Stepp
slides created by Marty Stepp
CSE 373: Data Structures and Algorithms
Lecture 3: Maps and Iterators
Generics, Stack, Queue Based on slides by Alyssa Harding
Stacks, Queues, and Deques
Lecture 3: Maps and Iterators
Lecture 2: Stacks and Queues
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

Lecture 2: Stacks and Queues CSE 373: Data Structures and Algorithms CSE 373 19 wi - Kasey Champion

Warm Up Grab a worksheet Introduce yourself to your neighbors  Discuss the answers Log onto www.socrative.com Click “student login” Enter “CSE373” as a room name Enter your name Last, First Answer question Get extra credit! CSE 373 19 WI - Kasey Champion

List ADT tradeoffs Time needed to access i-th element: Array: O(1) constant time LinkedList: O(n) linear time Time needed to insert at i-th element Array: O(n) linear time Amount of space used overall Array: sometimes wasted space LinkedList: compact Amount of space used per element Array: minimal LinkedList: tiny extra char[] myArr = new char[5] 1 2 3 4 ‘h’ ‘e’ ‘l’ ‘o’ LinkedList<Character> myLl = new LinkedList<Character>(); ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ / front CSE 373 19 wi - Kasey Champion

Design Decisions Discuss with your neighbors: How would you implement the List ADT for each of the following situations? For each consider the most important functions to optimize. Situation #1: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist. LinkedList – optimize for growth of list and movement of songs Situation #2: Write a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions. ArrayList – optimize for addition to back and accessing of elements Situation #3: Write a data structure that implements the List ADT that will be used to store the order of students waiting to speak to a TA at a tutoring center LinkedList - optimize for removal from front ArrayList – optimize for addition to back CSE 373 19 wi - Kasey Champion

Review: What is a Stack? stack: A collection based on the principle of adding elements and retrieving them in the opposite order. Last-In, First-Out ("LIFO") Elements are stored in order of insertion. We do not think of them as having indexes. Client can only add/remove/examine the last element added (the "top"). push pop, peek top 3 2 bottom 1 Stack ADT push(item) add item to top pop() return and remove item at top peek() look at item at top size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items supported operations: push(item): Add an element to the top of stack pop(): Remove the top element and returns it peek(): Examine the top element without removing it size(): how many items are in the stack? isEmpty(): true if there are 1 or more items in stack, false otherwise CSE 143 SP 17 – Zora Fung

Implementing a Stack with an Array Stack ADT push(item) add item to top pop() return and remove item at top peek() look at item at top size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items ArrayStack<E> push data[size] = value, if out of room grow data pop return data[size - 1], size-1 peek return data[size - 1] size return size isEmpty return size == 0 state behavior data[] size Big O Analysis pop() peek() size() isEmpty() push() O(1) Constant O(1) Constant O(1) Constant O(1) Constant O(1) Constant or worst case O(N) linear 1 2 3 push(3) push(4) pop() push(5) 3 4 5 numberOfItems = 2 1 CSE 373 19 wi - Kasey Champion

Implementing a Stack with Nodes Stack ADT push(item) add item to top pop() return and remove item at top peek() look at item at top size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items LinkedStack<E> push add new node at top pop return and remove node at top peek return node at top size return size isEmpty return size == 0 state behavior Node top size Big O Analysis pop() peek() size() isEmpty() push() O(1) Constant O(1) Constant O(1) Constant O(1) Constant O(1) Constant 4 push(3) push(4) pop() 3 front numberOfItems = 1 2 CSE 373 19 WI - Kasey Champion

Review: What is a Queue? queue: Retrieves elements in the order they were added. First-In, First-Out ("FIFO") Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only examine/remove the front of the queue. front back 1 2 3 remove, peek add Queue ADT add(item) add item to back remove() remove and return item at front peek() return item at front size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items supported operations: add(item): aka “enqueue” add an element to the back. remove(): aka “dequeue” Remove the front element and return. peek(): Examine the front element without removing it. size(): how many items are stored in the queue? isEmpty(): if 1 or more items in the queue returns true, false otherwise CSE 143 SP 17 – Zora Fung

Implementing a Queue with an Array Queue ADT add(item) add item to back remove() remove and return item at front peek() return item at front size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items ArrayQueue<E> add – data[size] = value, if out of room grow data remove – return data[size - 1], size-1 peek – return data[size - 1] size – return size isEmpty – return size == 0 state behavior data[] Size front index back index Big O Analysis remove() peek() size() isEmpty() add() O(1) Constant O(1) Constant O(1) Constant O(1) Constant O(1) Constant or worst case O(N) linear Spend rest of class implementing queue – do we need to address circular queues? 1 2 3 4 add(5) add(8) add(9) remove() 5 8 9 numberOfItems = 3 1 2 front = 0 1 back = 0 2 1 CSE 373 19 wi - Kasey Champion

Implementing a Queue with an Array > Wrapping Around front back add(7) add(4) add(1) 1 2 3 4 4 5 9 2 7 front numberOfItems = 5 4 3 back 1 2 3 4 5 6 7 8 9 1 CSE 373 SP 18 - Kasey Champion

Implementing a Queue with Nodes Queue ADT add(item) add item to back remove() remove and return item at front peek() return item at front size() count of items isEmpty() count of items is 0? state behavior Set of ordered items Number of items LinkedQueue<E> add – add node to back remove – return and remove node at front peek – return node at front size – return size isEmpty – return size == 0 state behavior Node front Node back size Big O Analysis remove() peek() size() isEmpty() add() O(1) Constant O(1) Constant O(1) Constant O(1) Constant O(1) Constant Spend rest of class implementing queue – do we need to address circular queues? numberOfItems = 2 1 add(5) add(8) remove() front 5 8 back CSE 373 19 wi - Kasey Champion

Review: Generics // a parameterized (generic) class public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; // a parameterized (generic) class public class name<TypeParameter> { ... } Forces any client that constructs your object to supply a type Don't write an actual type such as String; the client does that Instead, write a type variable name such as E (for "element") or T (for "type") You can require multiple type parameters separated by commas The rest of your class's code can refer to that type by name public class Box<T> { private T t; public void set(T t) { this.t = t; } public T get() { return t; More details: https://docs.oracle.com/javase/tutorial/java/generics/types.html CSE 373 19 wi - Kasey Champion

Implementing a Generic Stack Spend 10 minutes implementing a stack CSE 373 SP 18 - Kasey Champion