queues
play

Queues Algorithms CSE 373 19 SU -- ROBBIE WEBER 1 Administrivia - PowerPoint PPT Presentation

Lecture 2: Stacks and CSE 373: Data Structures and Queues Algorithms CSE 373 19 SU -- ROBBIE WEBER 1 Administrivia Course Stuff - Office hours are on class webpage: cs.washington.edu/373 - Piazza: https://piazza.com/class/jwcann1clfq7bn -


  1. Lecture 2: Stacks and CSE 373: Data Structures and Queues Algorithms CSE 373 19 SU -- ROBBIE WEBER 1

  2. Administrivia Course Stuff - Office hours are on class webpage: cs.washington.edu/373 - Piazza: https://piazza.com/class/jwcann1clfq7bn - Add code is on Canvas (or ask a staff member) Project 0 Live! - Individual assignment - 14x content review - GitLab/IntelliJ setup - You should have already gotten an automatic email with a link to your gitlab repo. - Check your spam folder Project 1 out next week, partner project - find your own partner - Lecture, section, piazza, office hours Last 5-10 minutes of section will be help with gitlab/intelliJ setup (if you’re stuck bring your laptop and get some help.) CSE 373 19 SU -- ROBBIE WEBER 2

  3. Q: Would you use a LinkedList or ArrayList Warm Up implementation for each of these scenarios? Situation #1: Write a data Arr rrayLis ist Lin inkedLis ist Instructions structure that implements the List uses an Array as underlying storage uses nodes as underlying storage ADT that will be used to store a ArrayList<E> Take 3 3 Min inutes list of songs in a playlist. LinkedList<E> state 1. Introduce yourself to state data[] Situation #2: Write a data Node front size your neighbors  size behavior structure that implements the List behavior get return data[index] set data[index] = value get loop until index, ADT that will be used to store the 2. Discuss your answers append data[size] = return node’s value history of a bank customer’s set loop until index, value, if out of space 3. Log onto Poll grow data update node’s value transactions. insert shift values to append create new Everywhere make hole at index, node, update next of data[index] = value, if last node 1. Go to Situation #3: Write a data insert create new out of space grow data PollEv.com/cse373su19 delete shift following node, loop until structure that implements the List values forward index, update next 2. OR Text CSE373Su19 to size return size fields ADT that will be used to store the delete loop until 22333 to join session, 0 1 2 3 4 index, skip node order of students waiting to speak text “1” “2” or “3” to select size return size your answer 88.6 26.1 94.4 0 0 to a TA at a tutoring center 88.6 26.1 94.4 4. Get extra credit! free space list CSE 373 19 SU -- ROBBIE WEBER 3

  4. Design Decisions Situatio uation n #1: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist. ArrayLis List – I w want t to to be able le to to shuffle fle play y on the playlis ylist Situatio uation n #2: Write a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions. ArrayLis List – optimize imize for addition ition to to back k and acces essing ing of element ements Situatio uation n #3: 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 LinkedLis edList t - optimi mize ze for removal l from front nt ArrayLis List – optimiz imize e for addition ition to to back CSE 373 19 SU -- ROBBIE WEBER 4

  5. List ADT tradeoffs Last time: we used “slow” and “fast” to describe running times. Let’s be a little more precise. Recall these basic Big-O ideas from 14X: Suppose our list has N elements - If a method takes a constant number of steps (like 23 or 5) its running time is O(1) - If a method takes a linear number of steps (like 4N+3) its running time is O(N) For ArrayLists and LinkedLists, what is the O() for each of these operations? - Time needed to access 𝑂 th element: - Time needed to insert at end (the array is full!) What are the memory tradeoffs for our two implementations? - Amount of space used overall - Amount of space used per element ArrayList<Character> myArr LinkedList<Character> myLl 0 1 2 3 4 front ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘h’ ‘o’ / ‘e’ ‘l’ ‘l’ CSE 373 19 SU -- ROBBIE WEBER 5

  6. List ADT tradeoffs Time needed to access 𝑂 th element: - ArrayList: O(1) constant time ArrayList<Character> myArr - LinkedList: O(N) linear time 0 1 2 3 4 Time needed to insert at 𝑂 th element (the array is full!) ‘h’ ‘e’ ‘l’ ‘l’ ‘o’ - ArrayList: O(N) linear time - LinkedList: O(N) linear time Amount of space used overall LinkedList<Character> myLl - ArrayList: sometimes wasted space front - LinkedList: compact ‘h’ ‘o’ / ‘e’ ‘l’ ‘l’ Amount of space used per element - ArrayList: minimal - LinkedList: tiny extra CSE 373 19 SU -- ROBBIE WEBER 6

  7. Goals for Today Review Stacks, Queues - What are the ADTs - How can you implement both of them with arrays and with nodes? Basics of Testing your code. (maybe) Review Dictionaries. - What is the ADT - Can we implement well with arrays and nodes? CSE 373 19 SU - ROBBIE WEBER 7

  8. 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") push pop, peek - 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"). top 3 2 Stack ADT bottom 1 stat tate supported operations: Set of ordered items - push( sh(it item em): Add an element to the top of stack Number of items beh behavior - pop(): Remove the top element and returns it push(item) add item to top - peek() (): Examine the top element without removing it pop() return and remove item at top - size( e(): ): how many items are in the stack? peek() look at item at top - isEmpty mpty(): (): false if there are 1 or more items in stack, true otherwise size() count of items isEmpty() count of items is 0? CSE 143 SP 17 – ZORAH FUNG 8

  9. Implementing a Stack with an Array Stack ADT ArrayStack<E> Big O Big O Ana nalysis stat tate O(1) Constant state pop() data[] Set of ordered items O(1) Constant size Number of items peek() behavior behavior beh push data[numItems] = value, if O(1) Constant size() push(item) add item to top out of room grow data, numItems++ pop() return and remove pop return data[numItems - 1], O(1) Constant item at top isEmpty() numItems-=1 peek() look at item at top peek return data[numItems - 1] Don’t resize: O(1) Constant size() count of items push() size return numItems isEmpty() count of items is 0? Do resize: O(N) linear isEmpty return numItems == 0 0 1 2 3 push(3) 3 5 4 push(4) pop() 2 numItems = 0 1 push(5) CSE 373 19 SU - ROBBIE WEBER 9

  10. Implementing a Stack with Nodes Stack ADT LinkedStack<E> Big O Big O Ana nalysis O(1) Constant stat tate state pop() Node top Set of ordered items O(1) Constant size Number of items peek() behavior beh behavior O(1) Constant push add new node at top size() push(item) add item to top numItems++ pop() return and remove pop return and remove node at O(1) Constant isEmpty() item at top top, numItems-=1 peek() look at item at top peek return node at top O(1) Constant size() count of items push() size return numItems isEmpty() count of items is 0? isEmpty return numItems == 0 4 push(3) front 3 push(4) pop() numItems = 0 1 2 CSE 373 19 SU - ROBBIE WEBER 10

  11. Review: What is a Queue? queue ue: 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 remove, peek add 1 2 3 Queue ADT supported operations: stat tate Set of ordered items - add(i (item): em): aka “enqueue” add an element to the back. Number of items beh behavior - remove( e(): ): aka “dequeue” Remove the front element and return. add(item) add item to back - peek() (): Examine the front element without removing it. remove() remove and return item at front - size( e(): ): how many items are stored in the queue? peek() return item at front - isEmpty mpty(): (): if 1 or more items in the queue returns true, false otherwise size() count of items isEmpty() count of items is 0? CSE 373 19 SU - ROBBIE WEBER 11

  12. Implementing a Queue with an Array Queue ADT ArrayQueue<E> Big O Big O Ana nalysis O(1) Constant stat tate state remove() data[] Set of ordered items O(1) Constant numItems Number of items peek() front index beh behavior back index O(1) Constant size() add(item) add item to back behavior remove() remove and return add – data[back] = value, if out of O(1) Constant isEmpty() item at front room grow data, back++, numItems++ peek() return item at front remove – return data[front], Don’t resize: O(1) Constant size() count of items add() numItems-=1, front++ isEmpty() count of items is 0? Do resize: O(N) linear peek – return data[front] size – return numItems isEmpty – return numItems == 0 0 1 2 3 4 add(5) 5 8 9 add(8) numItems = 3 2 1 0 add(9) 1 front = 0 remove() back = 0 2 1 CSE 373 19 SU - ROBBIE WEBER 12

  13. Implementing a Queue with an Array > Wrapping Around front back add(7) add(4) 0 1 2 3 4 add(1) 4 5 9 2 7 front numItems = 3 4 5 back 0 1 2 3 4 5 6 7 8 9 1 5 9 2 7 4 CSE 373 19 SU - ROBBIE WEBER 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend