Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENEE150 Discussion 09 Section 0101 Adam Wang.

Similar presentations


Presentation on theme: "ENEE150 Discussion 09 Section 0101 Adam Wang."— Presentation transcript:

1 ENEE150 Discussion 09 Section 0101 Adam Wang

2 Overview Hash tables and Trees Project 3 Quiz

3 Hash Tables An array of linked lists (called buckets)
Each piece of data has a hash code This code gets mapped to an index in the array The entry gets added to the linked list Ideally, each linked list has very few nodes Very fast for looking up data pieces quickly Other variations: hashmap, treemap, linkedhashmap

4 Trees Like linked lists, except each node now has two pointers
Binary Search tree: Every child to the left is less than the parent, and every child to the right is greater than the parent When searching for an element, we only have to search on average log(N) nodes We’ll see later that it’s natural to use recursion for trees

5 Project 3: Bus Simulator
Simulates a bus moving through bus stops and adding/removing passengers The program will take in a passengers file and a log file The passenger file contains all the data about when each passenger will come and which bus stop they will get on/get off You will write to the log file who gets on/gets off at each time step You will use a counter to simulate time moving; think of it as printing out what has happened during each hour of the simulation

6 Sample passenger file First number is number of bus stops
Each line after that represents a passenger 1st number: arrival time (“arrival hour”) Ascending order since it’s a simulation 2nd number: Starting bus stop 3rd number: Destination bus stop Bus only goes forwards; some passengers have to go all the way around

7 Recommended data structures
First thing you’ll do is malloc an array of bus stops You know the size from the first number in the passengers file Then you’ll initialize the bus This doesn’t need to be malloc’d At each time step you’ll add/remove passengers to/from the bus/bus stop

8 Global variables you might want to use

9 Recommended methods

10 In main Open the passenger and log files to be read and written
argv[1] is the passenger file and argv[2] is the log file; “r” for read and “w” for write Read in number of bus stops and initialize array of bus stops, as well as the bus Each bus stop you have to initialize the ID as well While Loop: runs until done reading passenger file and no more passengers in the ENTIRE system Each iteration of the loop simulates one timestep/one “hour” When done, remember to print stats of the simulation

11 Inside while loop (continued)
Print to log file the status of each bus stop Use method fprintf 1st time step won’t have any passengers Read in new passengers from file that are arriving at this time Use fscanf The FILE* will recall where you last left off reading it at Remove any passengers getting off at this stop (disembark_bus) Add any passengers at this stop getting on (board_bus) Increase wait times of passengers at all the bus stops (age_passengers) Move bus to the next station Increase time counter

12 void initialize_system()
Malloc the array of bus stops Initialize the bus Remember to initialize all the fields to 0/NULL Each bus stop also has a unique ID representing its stop #

13 int get_passengers(FILE *inFile)
Reads in any passengers arriving at the current time step May be more than one, or none at all! For each passenger, malloc and initialize each of its fields Insert that passenger into the proper bus stop (insert_passenger) Increment total passengers, # waiting at that station, etc. Return value: whether any new passengers were read in Obviously this method can return whatever you want it to

14 void disembark_bus() Remove any passengers on the bus getting off at the current bus stop Iterate through passenger linked list If passenger’s destination is this stop, Remove from the list (delete_passenger) Decrement the bus's number of passengers Decrement total number of passengers Record wait time of passenger

15 void board_bus() Adds all passengers waiting at the current bus stop to the bus Look up list concatenation Remember: we can just copy over pointers, because memory has already been allocated We also need to increment # of passengers on the bus, and set number of passengers at bus stop to 0

16 void age_passengers()
Increment wait time of all the passengers waiting at each bus stop Simple loop for each bus stop, and loop for each passenger

17 void write_stops (FILE *logFile)
Writes the status of each bus stop to the log file Remember, the format has to match EXACTLY what’s in the expected output

18 Void insert_passenger (passenger **head, passenger *to_add)
Inserts “to_add” into the linked list “head” Make this easy for you: just insert at the head

19 Void delete_passenger (passenger **head, passenger *to_del)
Deletes “to_del” from the linked list You don’t have to follow this format; use the delete method from class if you want You don’t even need this separate method at all

20 void write_stats(FILE *logFile)
Writes final stats of the simulation Make use of global variables sim_time, total_passengers, etc. for this

21 Extra Only the passenger linked lists and the array of bus stops needs to be dynamically allocated Up to you what to do with the bus and each bus stop Lucky for you, you are not required to free any memory Add to the head of the linked list whenever you can (easy) Hardest part is just keeping track of all the variables

22 Quiz wget ece.umd.edu/~aw97/quizzes/quiz3.c
Dynamically allocate a struct and insert it into the tail of a linked list submit 2017 fall enee quiz3.c


Download ppt "ENEE150 Discussion 09 Section 0101 Adam Wang."

Similar presentations


Ads by Google