150 likes | 280 Views
Recitation April 3, 2014. Project 3 Due on Wednesday, April 14th Get started ASAP! Quiz 2 and Homework 2 grades are posted Extra office hours: 3-5pm Next week: bring questions about Project 3 !. Announcements. Events within each time stamp: Passengers arrive at their stations
E N D
Project 3 Due on Wednesday, April 14th • Get started ASAP! • Quiz 2 and Homework 2 grades are posted • Extra office hours: 3-5pm • Next week: bring questions about Project 3! Announcements
Events within each time stamp: • Passengers arrive at their stations • Trains move one station “up” or “down” • Passengers disembark • Passengers board train Project 3 Implementation
Stations data structure: • Array of pointers to structs • Each struct needs: • Station name • Number of passengers waiting (total) • 2 pointers to linked lists of passengers • One going up, one going down Stations
Caveats: • There can be more than one train line • You don’t need to use ‘malloc’ continuously! • Example Pseudo code: Assuming num_line and num_station were scanned in from the stations file: lines=(station **)malloc(sizeof(station *)*num_line); for(j=0;j<num_line:j++){ lines[j]=(station *)malloc(sizeof(station)*num_station); for(i=0;i<num_station;i++) fscanf(infile, “%[^\n]s”,(lines[j]+i)->name); Stations
Passenger file Example: 1 20 2 0 151 10 1 7 Key: Arrival time, number of hops, arrival line number, arrival station number, departure line number, departure station number, repeats Passenger Arrival
Arrival time Hop # Routing data Passenger Struct
Start by scanning in a passenger: • Be careful! Consider creating a buffer for your passengers- you don’t know a passenger’s time stamp until you’ve scanned him! • Malloc space for the passenger • Use number of hops to malloc an array of travel information of the correct size • Perform tail insertion at specified station! Passenger Arrival
Requires you to track down the tail: • for (cur = *head; cur->next; cur = cur->next); cur->next = p; p->next = NULL; • Don’t forget to make sure that the list’s head pointer is not NULL • If it is, perform head insertion! p->next = *head; *head = p; Tail Insertion
Each line has 2 trains • Travel in opposite directions • Start at opposite ends • Reverse direction at the same time • Move 1 station per time stamp Line # Current station # of riders Rider list ?? direction Trains
At every time stamp, search through the rider list to check for riders with station line and number that match the train’s position. • If you find them- update statistics, decrement number of passengers, then delete them! Unloading passengers
Example Pseudo code: prev= *head; cur = (*head)->next; for (; cur->dest_station!=train->cur_station; ) { prev = prev->next; cur = cur->next; } prev->next = cur->next; Deletion
Load from the head of the station’s list of passengers • Remember- there’s a 100 rider limit! • Perform head deletion on station’s list, tail insertion on train’s list. • Only load passengers onto the train if it’s going in the correct direction! Loading passengers
Keep track of the passenger pointer when moving from station to train. • Use a temporary pointer to make the transfer while modifying the head pointer of the station list. Follow that pointer!
Handout 22 on the ENEE150 website • Contains with single examples of: • Declaring linked data structures • Head, tail, middle insertion • Head, tail, middle deletion List.c