1 / 15

Recitation April 3, 2014

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

finnea
Download Presentation

Recitation April 3, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation April 3, 2014

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Arrival time Hop # Routing data Passenger Struct

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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!

  15. 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

More Related