Download presentation
Presentation is loading. Please wait.
Published byMagnus Lawrence Modified over 9 years ago
1
CS1020 Week 9: 19 th March 2015
2
Contents Practice Exercise 33 Practice Exercise 34 Take-home Lab #4 Week 92
3
Practice Exercise #33 QuickEat – Queue Implementation Week 93
4
QuickEat (1/3) Week 9 Problem: The food is to be served to customers on first-come-first- serve basis The dishes are served only when ready There are a limited amount of dishes Dishes Ready but No Order Throw Away !! 4
5
QuickEat (2/3) Week 9 Dish1 Tag Dish 2 Tag Dish 3 Tag Each Dish Has Own Queue Maintain ArrayList Of Dish Queues Add More Dishes 5
6
QuickEat (3/3) Week 9 Solution : For Each Dish – maintain a queue for the Orders Process the Input correctly : Order (#Tag) (#DishOrdered) (#DishId) ….. Ready (#DishId) For every order operation add to queue For every ready operation process the queue and serve to customer 6
7
Understand Question Week 9 Things to take note: Queue is an interface, not a class cannot be instantiated Maintain an arraylist for queue of all dishes Use LinkedList class which has implementation of methods in Queue interface dishQueues.add(new LinkedList ()); Queue Operation Use only methods in LinkedList class that are legal for Queue Eg – isEmpty(), offer(), poll() 7
8
Practice Exercise #34 WebBrowser – Stack Implementation Week 98
9
WebBrowser (1/4) Week 9 Problem: Maintain the browsing history of User User can move through the list websites viewed by “BACKWRD” and “FORWARD” commands Store every new website visited in the browsing history Print the final browsing history earliest latest page as well as the current page 9
10
WebBrowser (2/4) Week 9 Page Backward StackForward Stack Current Page Current Page changes to recently visited URL Back Operation – Pop back stack – modify current Forward Operation – Pop forward stack – modify current 10
11
WebBrowser (3/4) Week 9 Solution : Maintain two separate stacks – Back And Forward Check for corner case – when first page loads Empty Forward Stack for each new page load “BACKWARD” – Pop from back stack move current forward “FORWARD” – Pop from forward stack move current backward Print – Earliest to Latest page history Also print the current page 11
12
WebBrowser (4/4) Week 9 Things to take note: Make use of Java Stack API Alternatively, solution can be achieved using LinkedList ADT Can try it to have practice on LinkedList Familiarize with Stack API to prepare better for SitIn Lab 3 12
13
Take-home Lab #4 Exercise 1 – Cargo Optimization Week 913
14
Cargo Optimization (1/3) Week 9 Problem: Containers arrives at the terminal in a random order, addressed to different destination ships (A-Z) They need to be stacked at the terminal The destination ships arrives at the terminal in a fixed sequence (A-Z) 14
15
Terminal Cargo Optimization (2/3) Week 9 Random order To: C To: A To: D To: B 15
16
Cargo Optimization (3/3) Week 9 Terminal Fixed order 16
17
Required Solution Week 9 Minimally, how many stacks are needed, such that destination ship has all its cargo is at the top when it arrives? Hint: You should stack container #1 on top of another container #2 only if Container #1’s ship arrive before or at the same time as container #2 You should always choose the stack whose top container’s ship’s arrival time is closest with the new container’s ship’s arrival time 17
18
Take-home Lab #4 Exercise 2 – Queue Simulation Week 918
19
Objective is to find the final queue resulting from VIP customers and Regular Customers in Queue and find the number of customers before the specified customer Solution Always insert a VIP customer before a regular customer in the queue Redirect the links during insertion is a pre-requisite Queue is always first come first serve. This questions requires a modified priority queue. Queue Simulation Week 919
20
Queue Visualization (VIP customer enters the Queue) (1/4) QueueSimulation VIP 1 Reg 2Reg 3Reg 1 head First VIP Customer Arrived: Week 920
21
Queue Visualization (VIP customer enters the Queue) (2/4) QueueSimulation VIP 1 Reg 2Reg 3Reg 1 head Week 921
22
Queue Visualization (VIP customer enters the Queue) (3/4) QueueSimulation VIP 1 Reg 2Reg 3Reg 1 head VIP 2 VIP 2 customers arrived: Week 922
23
Queue Visualization (VIP customer enters the Queue) (4/4) QueueSimulation VIP 1 Reg 2Reg 3Reg 1 head VIP 2 Week 923
24
Recap Take Home Lab 4 Due: 10pm - 20 th March Familiarize with Stack and Queue API Question?? Week 924
25
END OF FILE Week 925
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.