computer programming

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty - PowerPoint PPT Presentation

IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Programming using Structures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 1


  1. IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Programming using Structures Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 1

  2. Quic ick Recap of f Rele levant Topics IIT Bombay • Structures as collections of variables/arrays/other structures • Pointers to structures • Accessing members of structures • Linked structures • Dynamic allocation/de-allocation of structures 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

  3. Overv rview of f Th This is Le Lecture IIT Bombay • Example C++ program using structures • Linked structures • Accessing members through “.” and “ - >” • Dynamic allocation/de-allocation of structures 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

  4. Acknowledgment IIT Bombay • Some examples in this lecture are from An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 • All such examples indicated in slides with the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

  5. A Taxi Queuing System [In [Inspired by AGRBook] IIT Bombay • Taxis at a train station arrive and depart at different times • We want to maintain a queue of taxis at the train station • The queue implements a first-in-first-out policy for taxis • The taxi that came to the station earliest (among all waiting taxis) is at the “front” of the queue. • The taxi at the “front” of the queue is the next one to be dispatched. • The latest taxi to arrive at the station joins the queue at its “end”. • No taxi leaves the queue once it joins at the “end” until it reaches the “front”, and is dispatched. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5

  6. A Taxi Queuing System: St Structures Used IIT Bombay All structure definitions #include <iostream> before and outside using namespace std; definitions of functions that use them struct Driver {string name; int id;}; struct LinkedTaxi {int id; Driver *drv; LinkedTaxi *next;}; struct Queue {LinkedTaxi *front, *end; int numTaxis;}; … Rest of program file … Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

  7. A Taxi Queuing System: St Structures Used IIT Bombay Note the use of “string” data type #include <iostream> instead of “char” array . Helps simplify reading of strings with using namespace std; spaces using “ cin ” struct Driver {string name; int id;}; struct LinkedTaxi {int id; Driver *drv; LinkedTaxi *next;}; struct Queue {LinkedTaxi *front, *end; int numTaxis;}; … Rest of program file … Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7

  8. A Taxi Queuing System: In Init itializing Th The Queue IIT Bombay #include <iostream> using namespace std; … Structure definitions … int main() { Queue q; q.front = NULL; q.end = NULL; q.numTaxis = 0; … Rest of code … } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

  9. A Taxi Queuing System: Main in Lo Loop IIT Bombay while (true) { cout << “Command: ‘j’ to join queue, ‘d’ to dispatch, ’x’ to exit.” << endl; char command; cin >> command; switch(command) { case ‘j’: … Code to add a newly arrived taxi at end of queue … break; case ‘d’: … Code to dispatch taxi at front of queue … break; case ‘x’: cout << “Thank you” << endl; return 0; default: cout << “Invalid command.” << endl; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9

  10. Adding a Taxi at “end” of Queue IIT Bombay Driver *newDrv; newDrv = new Driver; if (newDrv == NULL) {cout << “Memory allocation failure” << endl; return -1;} cout << “Give name of driver: “; cin >> newDrv->name; cout << “Give id of driver: “; cin >> newDrv->id; LinkedTaxi *newTaxi; newTaxi = new LinkedTaxi; if (newTaxi == NULL) {cout << “Memory allocation failure”<< endl; return -1;} newTaxi->drv = newDrv; newTaxi->next = NULL; cout << “Give id of taxi: “; cin >> newTaxi->id; if (q.end == NULL) { // Taxi queue empty q.front = newTaxi; q.end = newTaxi; q.numTaxis = 1; } else {(q.end)->next = newTaxi; q.end = newTaxi; q.numTaxis++; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

  11. Adding a Taxi at “end” of Queue IIT Bombay Driver *newDrv; newDrv = new Driver; if (newDrv == NULL) {cout << “Memory allocation failure” << endl; return -1;} cout << “Give name of driver: “; cin >> newDrv->name; cout << “Give id of driver: “; cin >> newDrv->id; LinkedTaxi *newTaxi; newTaxi = new LinkedTaxi; if (newTaxi == NULL) {cout << “Memory allocation failure”<< endl; return -1;} newTaxi->drv = newDrv; newTaxi->next = NULL; next next next cout << “Give id of taxi: “; cin >> newTaxi->id; NULL NULL if (q.end == NULL) { // Taxi queue empty q.front = newTaxi; q.end = newTaxi; q.numTaxis = 1; q.end q.front newTaxi } else {(q.end)->next = newTaxi; q.end = newTaxi; q.numTaxis++; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11

  12. Adding a Taxi at “end” of Queue IIT Bombay Driver *newDrv; newDrv = new Driver; if (newDrv == NULL) {cout << “Memory allocation failure” << endl; return -1;} cout << “Give name of driver: “; cin >> newDrv->name; cout << “Give id of driver: “; cin >> newDrv->id; LinkedTaxi *newTaxi; newTaxi = new LinkedTaxi; if (newTaxi == NULL) {cout << “Memory allocation failure”<< endl; return -1;} newTaxi->drv = newDrv; newTaxi->next = NULL; next next next cout << “Give id of taxi: “; cin >> newTaxi->id; NULL if (q.end == NULL) { // Taxi queue empty q.front = newTaxi; q.end = newTaxi; q.numTaxis = 1; q.end q.front newTaxi } else {(q.end)->next = newTaxi; q.end = newTaxi; q.numTaxis++; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

  13. Dispatching a Taxi from “front” of Queue IIT Bombay if (q.front == NULL) {cout << “Sorry! No taxis in queue at present!” << endl;} else { dispatchTaxi LinkedTaxi *dispatchTaxi; dispatchTaxi = q.front; if (q.front == q.end) { // Only one taxi in queue next next next q.front = NULL; q.end = NULL; q.numTaxis = 0; NULL } else {q.front = (q.front)->next; q.numTaxis--;} q.front q.end if (dispatchTaxi != NULL) { cout << “Dispatching taxi with id “ << dispatchTaxi->id << endl; if (dispatchTaxi->drv != NULL) {delete dispatchTaxi->drv;} delete dispatchTaxi; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13

  14. Dispatching a Taxi from “front” of Queue IIT Bombay if (q.front == NULL) {cout << “Sorry! No taxis in queue at present!” << endl;} else { dispatchTaxi LinkedTaxi *dispatchTaxi; dispatchTaxi = q.front; if (q.front == q.end) { // Only one taxi in queue next next next q.front = NULL; q.end = NULL; q.numTaxis = 0; NULL } else {q.front = (q.front)->next; q.numTaxis--;} q.front q.end if (dispatchTaxi != NULL) { cout << “Dispatching taxi with id “ << dispatchTaxi->id << endl; if (dispatchTaxi->drv != NULL) {delete dispatchTaxi->drv;} delete dispatchTaxi; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

  15. Dispatching a Taxi from “front” of Queue IIT Bombay if (q.front == NULL) {cout << “Sorry! No taxis in queue at present!” << endl;} else { dispatchTaxi LinkedTaxi *dispatchTaxi; dispatchTaxi = q.front; if (q.front == q.end) { // Only one taxi in queue next next next q.front = NULL; q.end = NULL; q.numTaxis = 0; NULL } else {q.front = (q.front)->next; q.numTaxis--;} q.front q.end if (dispatchTaxi != NULL) { cout << “Dispatching taxi with id “ << dispatchTaxi->id << endl; if (dispatchTaxi->drv != NULL) {delete dispatchTaxi->drv;} delete dispatchTaxi; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15

  16. Su Summary IIT Bombay • A taxi dispatch system implementing a first-in-first-out order (queue) • Use of • Linked structures • Dynamic allocation/de-allocation of structures • Accessing members through “.” and “ - >” operators • Size of queue not pre-determined • Amount of memory in system dictates this Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16

Recommend


More recommend