473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

buy ticket (with link list and struct)

1
Hello,
I write a program to c++,for buy bus ticket.
with link list and struct.
my program is bad working,please help me to work better(up to now)?!
and tell how do i write
search and show travels between two dates,
and search and show travels with specified origin and destination in specified date,
in this program.
thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <conio.h>
  4. #include <process.h>
  5. #include <string.h>
  6.  
  7.  
  8. //-----------------------------------------------------
  9. //-----------------------------------------------------
  10.  
  11. struct Bus
  12. {
  13.     char* BusID;
  14.     char* Type;
  15.     int SeatNumber;
  16. };
  17.  
  18.  
  19. //-----------------------------------------------------
  20. //-----------------------------------------------------
  21.  
  22. struct Passenger
  23. {
  24.     int PassengerID;
  25.     char* Name;
  26.     char* Family;
  27.     char* TellNumber;    
  28. };
  29.  
  30.  
  31. //-----------------------------------------------------
  32. //-----------------------------------------------------
  33.  
  34. struct Date
  35. {
  36.     int Year;
  37.     int Month;
  38.     int Day;
  39. };
  40.  
  41.  
  42. //-----------------------------------------------------
  43. //-----------------------------------------------------
  44.  
  45.  
  46. struct Travel
  47. {
  48.     char* Origin;
  49.     char* Destination;
  50.     char* BusID;
  51.     Date date;
  52.     bool State ;
  53.     int* Passengers;
  54. };
  55.  
  56.  
  57.  
  58. //-----------------------------------------------------
  59. //-----------------------------------------------------
  60.  
  61.  
  62. struct BuyTicketNode
  63. {
  64.     Bus bus;
  65.     Passenger passenger;
  66.     Travel travel;
  67.     Date date;
  68.     BuyTicketNode* next;
  69. };
  70.  
  71.  
  72. //-----------------------------------------------------
  73. //-----------------------------------------------------
  74.  
  75.  
  76. struct BuyTicketLinkedList
  77. {
  78.     BuyTicketNode* root;
  79. };
  80.  
  81.  
  82. //-----------------------------------------------------
  83. //----------------------------------------------------
  84.  
  85. void uiDoAction(BuyTicketLinkedList &);
  86. char uiMainMenu();
  87. void uiMainMenuSwitch(char ,BuyTicketLinkedList &);
  88.  
  89. char uiBusMenu();
  90. void uiBusMenuSwitch(char ,BuyTicketLinkedList &);
  91.  
  92. void uiEditBus(BuyTicketLinkedList &);
  93. void uiDeleteBus(BuyTicketLinkedList &);
  94. void uiDisplayBus(BuyTicketLinkedList &);
  95.  
  96. void uiAddBus(BuyTicketLinkedList &);
  97. void uiSearchByBusID(BuyTicketLinkedList &);
  98. Bus uiGetBus();
  99. void AddBus(Bus ,BuyTicketLinkedList &);
  100. void uiDisplay(Bus bus);
  101. BuyTicketNode* searchByByBusID(char* ,BuyTicketLinkedList &);
  102. int countBus(BuyTicketLinkedList &);
  103. void removeNodeBus(BuyTicketNode* ,BuyTicketLinkedList &);
  104. void releaseBus(BuyTicketNode*);
  105. void releaseBus(BuyTicketLinkedList &);
  106.  
  107. char uiTravelMenu();
  108. void uiTravelMenuSwitch(char ,BuyTicketLinkedList &);
  109.  
  110. void uiEditTravel(BuyTicketLinkedList &);
  111. void uiDeleteTravel(BuyTicketLinkedList &);
  112. void uiDisplayTravel(BuyTicketLinkedList &);
  113.  
  114. void uiAddTravel(BuyTicketLinkedList &);
  115. void uiSearchByBusID(BuyTicketLinkedList &);
  116. Travel uiGetTravel();
  117. void AddTravel(Travel ,BuyTicketLinkedList &);
  118. void uiDisplay(Travel travel);
  119. BuyTicketNode* searchByByBusID(char* ,BuyTicketLinkedList &);
  120. int countTravel(BuyTicketLinkedList &);
  121. void removeNodeTravel(BuyTicketNode* ,BuyTicketLinkedList &);
  122. void releaseTravel(BuyTicketNode*);
  123. void releaseTravel(BuyTicketLinkedList &);
  124.  
  125. char uiPassengerMenu();
  126. void uiPassengerMenuSwitch(char ,BuyTicketLinkedList &);
  127. void uiAddPassenger(BuyTicketLinkedList &);
  128. void uiDisplayPassenger(BuyTicketLinkedList &);
  129.  
  130. void uiSearchByTellNumber(BuyTicketLinkedList &);
  131. Passenger uiGetPassenger();
  132. void AddPassenger(Passenger ,BuyTicketLinkedList &);
  133. void uiDisplay(Passenger passenger);
  134. BuyTicketNode* searchByByTellNumber(char* ,BuyTicketLinkedList &);
  135. int countPassenger(BuyTicketLinkedList &);
  136.  
  137. /*char uiSearchMenu();
  138. void uiSearchMenuSwitch(char ,BuyTicketLinkedList &);
  139. void uiSearchTravelsByDates(BuyTicketLinkedList);
  140. void uiDisplayTravelsByDates(BuyTicketLinkedList);
  141. void uiSearchTravelsByPlaces(BuyTicketLinkedList);
  142. void uiDisplayTravelsByPlaces(BuyTicketLinkedList);*/
  143.  
  144. //-----------------------------------------------------
  145. //-----------------------------------------------------
  146.  
  147. int main()
  148. {
  149.     BuyTicketLinkedList buyTicketLinkedList;
  150.     buyTicketLinkedList.root=NULL;
  151.     uiDoAction(buyTicketLinkedList);
  152.     return 0;
  153. }
  154.  
  155.  
  156. //-----------------------------------------------------
  157. //-----------------------------------------------------
  158.  
  159.  
  160. void uiDoAction(BuyTicketLinkedList &buyTicketLinkedList)
  161. {
  162.     do
  163.     {
  164.         char mainmenu=uiMainMenu();
  165.         uiMainMenuSwitch(mainmenu,buyTicketLinkedList);
  166.  
  167.         char busmenu=uiBusMenu();
  168.         uiBusMenuSwitch(busmenu,buyTicketLinkedList);
  169.  
  170.         char travelmenu=uiTravelMenu();
  171.         uiTravelMenuSwitch(travelmenu,buyTicketLinkedList);
  172.  
  173.         char passengermenu=uiPassengerMenu();
  174.         uiPassengerMenuSwitch(passengermenu,buyTicketLinkedList);
  175.  
  176.     /*    char searchmenu=uiSearchMenu();
  177.         uiSearchMenuSwitch(searchmenu,buyTicketLinkedList);
  178.     */    
  179.     }
  180.     while(true);
  181. }
  182.  
  183.  
  184. //-----------------------------------------------------
  185. //-----main menu---------------------------------------
  186.  
  187.  
  188. char uiMainMenu()
  189. {
  190.     cout << "\n\n================================================\n";
  191.     cout << "Main Menu ======================================\n";
  192.     cout << endl;
  193.     cout << "1> Bus"<<endl;
  194.     cout << "2> Travel"<<endl;
  195.     cout << "3> Passenger"<<endl;
  196.     cout << "4> Search"<<endl;
  197.     cout << "5> Exit"<<endl;
  198.  
  199.     char mainmenu=getch();
  200.     system("CLS");
  201.     return mainmenu;
  202. }
  203.  
  204.  
  205. //-----------------------------------------------------
  206. //-----------------------------------------------------
  207.  
  208.  
  209. void uiMainMenuSwitch(char selector,BuyTicketLinkedList &buyTicketLinkedList)
  210. {
  211.     switch(selector)
  212.     {
  213.     case '1':
  214.         uiBusMenu();
  215.         break;
  216.     case '2':
  217.         uiTravelMenu();
  218.         break;
  219.     case '3':
  220.         uiPassengerMenu();
  221.         break;
  222. /*    case '4':
  223.         uiSearchMenu();
  224.         break;*/
  225.     case '5':
  226.         releaseBus(buyTicketLinkedList);
  227.         exit(0);
  228.         break;
  229.     default:
  230.         break;
  231.     }
  232. }
  233.  
  234. //-----------------------------------------------------
  235. //----------bus menu-----------------------------------
  236.  
  237. char uiBusMenu()
  238. {    
  239.     cout << "\n\n================================================\n";
  240.     cout << "Bus Menu ======================================\n";
  241.     cout << endl;    
  242.     cout << "1>Add"<<endl;
  243.     cout << "2>Edit"<<endl;
  244.     cout << "3>Delete"<<endl;
  245.     cout << "4>Display"<<endl;
  246.     cout << "5>Go back to main menu"<<endl;
  247.  
  248.  
  249.     char busmenu=getch();
  250.     system("CLS");
  251.     return busmenu;
  252. }
  253.  
  254.  
  255. //-----------------------------------------------------
  256. //-----------------------------------------------------
  257.  
  258.  
  259. void uiBusMenuSwitch(char selector,BuyTicketLinkedList &buyTicketLinkedList)
  260. {
  261.     switch(selector)
  262.     {
  263.     case '1':
  264.         uiAddBus(buyTicketLinkedList);
  265.         break;
  266.     case '2':
  267.         uiEditBus(buyTicketLinkedList);
  268.         break;
  269.     case '3':
  270.         uiDeleteBus(buyTicketLinkedList);
  271.         break;
  272.     case '4':
  273.         uiDisplayBus(buyTicketLinkedList);
  274.         break;
  275.     case '5':
  276.         uiMainMenu();
  277.         break;
  278.     default:
  279.         break;
  280.     }
  281. }
  282. //-----------------------------------------------------
  283. //----------travel menu--------------------------------
  284.  
  285.  
  286. char uiTravelMenu()
  287. {    
  288.     cout << "\n\n================================================\n";
  289.     cout << "TravelMenu ======================================\n";
  290.     cout << endl;    
  291.     cout << "1>Add"<<endl;
  292.     cout << "2>Edit"<<endl;
  293.     cout << "3>Delete"<<endl;
  294.     cout << "4>Display"<<endl;
  295.     cout << "5>Go back to main menu"<<endl;
  296.  
  297.  
  298.     char travelmenu=getch();
  299.     system("CLS");
  300.     return travelmenu;
  301. }    
  302.  
  303.  
  304. //-----------------------------------------------------
  305. //-----------------------------------------------------
  306.  
  307.  
  308. void uiTravelMenuSwitch(char selector,BuyTicketLinkedList &buyTicketLinkedList)
  309. {
  310.     switch(selector)
  311.     {
  312.     case '1':
  313.         uiAddTravel(buyTicketLinkedList);
  314.         break;
  315.     case '2':
  316.         uiEditTravel(buyTicketLinkedList);
  317.         break;
  318.     case '3':
  319.         uiDeleteTravel(buyTicketLinkedList);
  320.         break;
  321.     case '4':
  322.         uiDisplayTravel(buyTicketLinkedList);
  323.         break;
  324.     case '5':
  325.         uiMainMenu();
  326.         break;
  327.     default:
  328.         break;
  329.     }
  330. }
  331.  
  332. //-----------------------------------------------------
  333. //---------passenger menu------------------------------
  334.  
  335.  
  336. char uiPassengerMenu()
  337. {    
  338.     cout << "\n\n================================================\n";
  339.     cout << "PassengerMenu ======================================\n";
  340.     cout << endl;    
  341.     cout << "1>Add"<<endl;
  342.     cout << "2>Display"<<endl;
  343.     cout << "3>Go back to main menu"<<endl;
  344.  
  345.     char passengermenu=getch();
  346.     system("CLS");
  347.     return passengermenu;
  348. }
  349.  
  350.  
  351. //-----------------------------------------------------
  352. //-----------------------------------------------------
  353.  
  354.  
  355. void uiPassengerMenuSwitch(char selector,BuyTicketLinkedList &buyTicketLinkedList)
  356. {
  357.     switch(selector)
  358.     {
  359.     case '1':
  360.         uiAddPassenger(buyTicketLinkedList);
  361.         break;
  362.     case '2':
  363.         uiDisplayPassenger(buyTicketLinkedList);
  364.         break;
  365.     case '3':
  366.         uiMainMenu();
  367.         break;
  368.     default:
  369.         break;
  370.     }
  371. }
  372.  
  373. //-----------------------------------------------------
  374. //-------search menu-----------------------------------
  375.  
  376. /*
  377. char uiSearchMenu()
  378. {
  379.     cout << "\n\n================================================\n";
  380.     cout << "PassengerMenu ======================================\n";
  381.     cout << endl;    
  382.     cout << "1> Search travels between two dates"<<endl;
  383.     cout << "2> Display travels between two dates"<<endl;
  384.     cout << "3> Search travels with specified origin and destination in specified date"<<endl;
  385.     cout << "4> Display travels with specified origin and destination in specified date"<<endl;
  386.     cout << "5>Go back to main menu"<<endl;
  387.  
  388.     char searchmenu=getch();
  389.     system("CLS");
  390.     return searchmenu;
  391. }
  392.  
  393.  
  394. //-----------------------------------------------------
  395. //-----------------------------------------------------
  396.  
  397.  
  398. void uiSearchMenuSwitch(char selector,BuyTicketLinkedList &buyTicketLinkedList)
  399. {
  400.     switch(selector)
  401.     {
  402.     case '1':
  403.         uiSearchTravelsByDates(buyTicketLinkedList);
  404.         break;
  405.     case '2':
  406.         uiDisplayTravelsByDates(buyTicketLinkedList);
  407.         break;
  408.     case '3':
  409.         uiSearchTravelsByPlaces(buyTicketLinkedList);
  410.         break;
  411.     case '4':
  412.         uiDisplayTravelsByPlaces(buyTicketLinkedList);
  413.         break;
  414.     case '5':
  415.         uiMainMenu();
  416.         break;
  417.     default:
  418.         break;
  419.     }
  420. }
  421. */
  422.  
  423. //--------------------------------------------------------------------------------------------------------------
  424. //------bus-----------------------------------------------------------------------------------------------------
  425. //--------------------------------------------------------------------------------------------------------------
  426.  
  427. void uiAddBus(BuyTicketLinkedList &buyTicketLinkedList)
  428. {
  429.     Bus newBus=uiGetBus();
  430.     AddBus(newBus,buyTicketLinkedList);
  431. }
  432.  
  433.  
  434. //-----------------------------------------------------
  435. //-----------------------------------------------------
  436.  
  437.  
  438.  
  439. void uiSearchByBusID(BuyTicketLinkedList &buyTicketLinkedList)
  440. {
  441.     const int BusIDSize = 20;
  442.     char searchKey[BusIDSize];
  443.  
  444.     cout << "Enter the ID of bus: " ;
  445.     cin >> searchKey;
  446.  
  447.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  448.  
  449.     if( result == NULL )
  450.         cout << "No such bus ID found!" << endl;
  451.     else
  452.         uiDisplay(result->bus);
  453.  
  454. }
  455.  
  456. //-----------------------------------------------------
  457. //-----------------------------------------------------
  458.  
  459.  
  460. int countBus(BuyTicketLinkedList &buyTicketLinkedList)
  461. {
  462.     BuyTicketNode* temp = buyTicketLinkedList.root;
  463.  
  464.     int counter = 0;
  465.  
  466.     while(temp != NULL)
  467.     {
  468.         counter++;
  469.         temp = temp->next;
  470.     }
  471.     return counter;
  472. }
  473.  
  474. //-----------------------------------------------------
  475. //-----------------------------------------------------
  476.  
  477. void uiEditBus(BuyTicketLinkedList &buyTicketLinkedList)
  478. {
  479.     const int BusIDSize = 20;
  480.     char searchKey[BusIDSize];
  481.  
  482.     cout << "Enter the ID of bus that you want to edit: " ;
  483.     cin >> searchKey;
  484.  
  485.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  486.  
  487.     if( result == NULL )
  488.         cout << "No such bus ID found!" << endl;
  489.     else
  490.     {
  491.         cout << "This bus ID is found: " << endl;
  492.         uiDisplay(result->bus);
  493.  
  494.         cout << "\nEnter new ID: ";
  495.         cin >> result->bus.BusID;
  496.     }
  497.  
  498. }
  499.  
  500. //-----------------------------------------------------
  501. //-----------------------------------------------------
  502.  
  503.  
  504. void uiDeleteBus(BuyTicketLinkedList &buyTicketLinkedList)
  505. {
  506.     const int BusIDSize = 15;
  507.     char searchKey[BusIDSize];
  508.  
  509.     cout << "Enter the ID of bus that you want to delete: " ;
  510.     cin >> searchKey;
  511.  
  512.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  513.  
  514.     if( result == NULL )
  515.         cout << "No such bus ID found!" << endl;
  516.     else
  517.     {
  518.         cout << "This bus is found: " << endl;
  519.         uiDisplay(result->bus);
  520.  
  521.         cout << "\nAre you sure to delete? (press y to accept or n to deny): ";
  522.         cout.flush();
  523.         if( getch() == 'y' )
  524.         {
  525.             removeNodeBus(result,buyTicketLinkedList);
  526.         }    
  527.     }
  528.  
  529. }
  530.  
  531. //-----------------------------------------------------
  532. //-----------------------------------------------------
  533.  
  534.  
  535. void removeNodeBus(BuyTicketNode* victim,BuyTicketLinkedList &buyTicketLinkedList)
  536. {
  537.     BuyTicketNode* temp = buyTicketLinkedList.root;
  538.     BuyTicketNode* temp2 = buyTicketLinkedList.root;
  539.     if(temp != NULL)
  540.     {
  541.         if( temp == victim )
  542.         {
  543.             buyTicketLinkedList.root = victim->next;
  544.             releaseBus(victim);
  545.         }
  546.         else
  547.             while(temp != NULL)
  548.             {
  549.                 if( temp == victim )
  550.                 {
  551.                     temp2->next = victim->next;
  552.                     releaseBus(victim);
  553.                     break;
  554.                 }
  555.                 temp2 = temp;
  556.                 temp = temp->next;
  557.             }
  558.     }
  559. }
  560.  
  561. //-----------------------------------------------------
  562. //-----------------------------------------------------
  563.  
  564. void releaseBus(BuyTicketNode* victim)
  565. {
  566.     delete[] victim->bus.BusID;
  567.     delete[] victim->bus.Type;
  568.     //delete[] victim->bus.SeatNumber;
  569. }
  570.  
  571. //-----------------------------------------------------
  572. //-----------------------------------------------------
  573.  
  574. void releaseBus(BuyTicketLinkedList &buyTicketLinkedList)
  575. {
  576.     BuyTicketNode* temp = buyTicketLinkedList.root;
  577.     BuyTicketNode* temp2;
  578.     while(temp != NULL)
  579.     {
  580.         temp2 = temp->next;
  581.         releaseBus(temp);
  582.         temp = temp2;
  583.     }
  584. }
  585.  
  586. //-----------------------------------------------------
  587. //-----------------------------------------------------
  588.  
  589. void uiDisplayBus(BuyTicketLinkedList &buyTicketLinkedList)
  590. {
  591.     BuyTicketNode* temp = buyTicketLinkedList.root;
  592.     if(temp == NULL)
  593.     {
  594.         cout << "The list is empty!" << endl;
  595.     }
  596.     else
  597.     {
  598.         cout << setw(25) << "Bus ID" << setw(25) << "Type" << setw(25) << "Seat Number" << endl;
  599.         cout << setw(25) << "------" << setw(25) << "-----" << setw(25) << "------------" << endl;
  600.         while(temp != NULL)
  601.         {
  602.             uiDisplay(temp->bus);
  603.             temp = temp->next;
  604.         }
  605.     }
  606. }
  607.  
  608. //-----------------------------------------------------
  609. //-----------------------------------------------------
  610.  
  611. Bus uiGetBus()
  612. {
  613.     const int BusIDSize=20;
  614.     const int TypeSize=20;
  615.  
  616.     Bus bus;
  617.     bus.BusID=new char[BusIDSize];
  618.     bus.Type=new char[TypeSize];
  619.  
  620.     cout<<"Enter type of bus: ";
  621.     cin>>bus.Type;
  622.  
  623.     cout<<"Enter seat number of bus: ";
  624.     cin>>bus.SeatNumber;
  625.  
  626.     cout<<"Enter ID of bus: ";
  627.     cin>>bus.BusID;
  628.  
  629.     return bus;
  630. }
  631.  
  632.  
  633.  
  634. //-----------------------------------------------------
  635. //-----------------------------------------------------
  636.  
  637. void AddBus(Bus newBus,BuyTicketLinkedList &buyTicketLinkedList)
  638. {
  639.     BuyTicketNode* buyTicketNode = new BuyTicketNode;
  640.     buyTicketNode->bus = newBus;
  641.     buyTicketNode->next = NULL;
  642.  
  643.     BuyTicketNode* temp = buyTicketLinkedList.root;
  644.     if(temp == NULL)
  645.     {
  646.         buyTicketLinkedList.root = buyTicketNode;
  647.     }
  648.     else
  649.     {
  650.         while(temp->next != NULL)
  651.             temp = temp->next;
  652.         temp->next = buyTicketNode;
  653.     }    
  654. }
  655.  
  656. //-----------------------------------------------------
  657. //-----------------------------------------------------
  658.  
  659. void uiDisplay(Bus bus)
  660. {
  661.     cout << setw(25) << bus.BusID << setw(25) << bus.Type << setw(25) << bus.SeatNumber << endl;
  662. }
  663.  
  664.  
  665. //-----------------------------------------------------
  666. //-----------------------------------------------------
  667.  
  668. BuyTicketNode* searchByByBusID(char* searchKey,BuyTicketLinkedList &buyTicketLinkedList)
  669. {
  670.     BuyTicketNode* temp = buyTicketLinkedList.root;
  671.     while(temp != NULL)
  672.     {
  673.         if( strcmp(searchKey,temp->bus.BusID) == 0 )
  674.             return temp;
  675.         temp = temp->next;
  676.     }
  677.     return NULL;
  678.  
  679. }
  680.  
  681.  
  682. //---------------------------------------------------------------------------------------------------------------------
  683. //------------travel---------------------------------------------------------------------------------------------------
  684. //---------------------------------------------------------------------------------------------------------------------
  685.  
  686.  
  687.  
  688. void uiAddTravel(BuyTicketLinkedList &buyTicketLinkedList)
  689. {
  690.     Travel newTravel=uiGetTravel();
  691.     AddTravel(newTravel,buyTicketLinkedList);
  692. }
  693.  
  694.  
  695. //-----------------------------------------------------
  696. //-----------------------------------------------------
  697.  
  698.  
  699. /*
  700. void uiSearchByBusID(BuyTicketLinkedList &buyTicketLinkedList)
  701. {
  702.     const int BusIDSize = 20;
  703.     char searchKey[BusIDSize];
  704.  
  705.     cout << "Enter the ID of bus: " ;
  706.     cin >> searchKey;
  707.  
  708.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  709.  
  710.     if( result == NULL )
  711.         cout << "No such bus ID found!" << endl;
  712.     else
  713.         uiDisplay(result->travel);
  714. }*/
  715.  
  716. //-----------------------------------------------------
  717. //-----------------------------------------------------
  718.  
  719.  
  720. int countTravel(BuyTicketLinkedList &buyTicketLinkedList)
  721. {
  722.     BuyTicketNode* temp = buyTicketLinkedList.root;
  723.  
  724.     int counter = 0;
  725.  
  726.     while(temp != NULL)
  727.     {
  728.         counter++;
  729.         temp = temp->next;
  730.     }
  731.     return counter;
  732. }
  733.  
  734. //-----------------------------------------------------
  735. //-----------------------------------------------------
  736.  
  737.  
  738. void uiEditTravel(BuyTicketLinkedList &buyTicketLinkedList)
  739. {
  740.     const int BusIDSize = 20;
  741.     char searchKey[BusIDSize];
  742.  
  743.     cout << "Enter the ID of bus that you want to edit: " ;
  744.     cin >> searchKey;
  745.  
  746.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  747.  
  748.     if( result == NULL )
  749.         cout << "No such bus ID found!" << endl;
  750.     else
  751.     {
  752.         cout << "This bus ID is found: " << endl;
  753.         uiDisplay(result->travel);
  754.  
  755.         cout << "\nEnter new ID: ";
  756.         cin >> result->travel.BusID;
  757.     }
  758.  
  759. }
  760.  
  761. //-----------------------------------------------------
  762. //-----------------------------------------------------
  763.  
  764.  
  765. void uiDeleteTravel(BuyTicketLinkedList &buyTicketLinkedList)
  766. {
  767.     const int BusIDSize = 15;
  768.     char searchKey[BusIDSize];
  769.  
  770.     cout << "Enter the ID of bus that you want to delete: " ;
  771.     cin >> searchKey;
  772.  
  773.     BuyTicketNode* result = searchByByBusID(searchKey,buyTicketLinkedList);
  774.  
  775.     if( result == NULL )
  776.         cout << "No such bus ID found!" << endl;
  777.     else
  778.     {
  779.         cout << "This bus is found: " << endl;
  780.         uiDisplay(result->travel);
  781.  
  782.         cout << "\nAre you sure to delete? (press y to accept or n to deny): ";
  783.         cout.flush();
  784.         if( getch() == 'y' )
  785.         {
  786.             removeNodeTravel(result,buyTicketLinkedList);
  787.         }    
  788.     }
  789.  
  790. }
  791.  
  792. //-----------------------------------------------------
  793. //-----------------------------------------------------
  794.  
  795.  
  796. void removeNodeTravel(BuyTicketNode* victim,BuyTicketLinkedList &buyTicketLinkedList)
  797. {
  798.     BuyTicketNode* temp = buyTicketLinkedList.root;
  799.     BuyTicketNode* temp2 = buyTicketLinkedList.root;
  800.     if(temp != NULL)
  801.     {
  802.         if( temp == victim )
  803.         {
  804.             buyTicketLinkedList.root = victim->next;
  805.             releaseTravel(victim);
  806.         }
  807.         else
  808.             while(temp != NULL)
  809.             {
  810.                 if( temp == victim )
  811.                 {
  812.                     temp2->next = victim->next;
  813.                     releaseBus(victim);
  814.                     break;
  815.                 }
  816.                 temp2 = temp;
  817.                 temp = temp->next;
  818.             }
  819.     }
  820. }
  821.  
  822. //-----------------------------------------------------
  823. //-----------------------------------------------------
  824.  
  825. void releaseTravel(BuyTicketNode* victim)
  826. {
  827.     delete[] victim->travel.Origin;
  828.     delete[] victim->travel.Destination;
  829.     delete[] victim->travel.BusID;
  830.     //delete[] victim->travel.date;
  831.     //delete[] victim->travel.Passengers;
  832. }
  833.  
  834. //-----------------------------------------------------
  835. //-----------------------------------------------------
  836.  
  837. void releaseTravel(BuyTicketLinkedList &buyTicketLinkedList)
  838. {
  839.     BuyTicketNode* temp = buyTicketLinkedList.root;
  840.     BuyTicketNode* temp2;
  841.     while(temp != NULL)
  842.     {
  843.         temp2 = temp->next;
  844.         releaseTravel(temp);
  845.         temp = temp2;
  846.     }
  847. }
  848.  
  849. //-----------------------------------------------------
  850. //-----------------------------------------------------
  851.  
  852. void uiDisplayTravel(BuyTicketLinkedList &buyTicketLinkedList)
  853. {
  854.     BuyTicketNode* temp = buyTicketLinkedList.root;
  855.     if(temp == NULL)
  856.     {
  857.         cout << "The list is empty!" << endl;
  858.     }
  859.     else
  860.     {
  861.         cout<<setw(20)<<"Bus ID"<< setw(10)<<"Origin"<<setw(10)<<"Destination"<< setw(10)<<"Date"<<setw(10)<<"Passenger"<< endl;
  862.         cout<<setw(20)<<"------"<< setw(10)<<"------"<<setw(10)<<"------------"<<setw(10)<<"----"<<setw(10)<<"---------"<< endl;
  863.         while(temp != NULL)
  864.         {
  865.             uiDisplay(temp->travel);
  866.             temp = temp->next;
  867.         }
  868.     }
  869. }
  870.  
  871. //-----------------------------------------------------
  872. //-----------------------------------------------------
  873.  
  874. Travel uiGetTravel()
  875. {
  876.     const int BusIDSize=20;
  877.     const int OriginSize=20;
  878.     const int DestinationSize=20;
  879.     //int date;
  880.     //int Passenger;
  881.  
  882.     Travel travel;
  883.     travel.BusID=new char[BusIDSize];
  884.     travel.Origin=new char[OriginSize];
  885.     travel.Destination=new char[DestinationSize];
  886.  
  887.     cout<<"Enter origin of travel: ";
  888.     cin>>travel.Origin;
  889.  
  890.     cout<<"Enter destination of travel: ";
  891.     cin>>travel.Destination;
  892.  
  893.     cout<<"Enter ID of bus: ";
  894.     cin>>travel.BusID;
  895.  
  896.     /*cout<<"Enter date of travel: ";
  897.     cin>>travel.date;*/
  898.  
  899.     /*cout<<"Enter number of passengers in travel: ";
  900.     cin>>travel.Passengers;*/
  901.  
  902.     return travel;
  903. }
  904.  
  905.  
  906.  
  907. //-----------------------------------------------------
  908. //-----------------------------------------------------
  909.  
  910. void AddTravel(Travel newTravel,BuyTicketLinkedList &buyTicketLinkedList)
  911. {
  912.     BuyTicketNode* buyTicketNode = new BuyTicketNode;
  913.     buyTicketNode->travel = newTravel;
  914.     buyTicketNode->next = NULL;
  915.  
  916.     BuyTicketNode* temp = buyTicketLinkedList.root;
  917.     if(temp == NULL)
  918.     {
  919.         buyTicketLinkedList.root = buyTicketNode;
  920.     }
  921.     else
  922.     {
  923.         while(temp->next != NULL)
  924.             temp = temp->next;
  925.         temp->next = buyTicketNode;
  926.     }    
  927. }
  928.  
  929. //-----------------------------------------------------
  930. //-----------------------------------------------------
  931.  
  932. void uiDisplay(Travel travel)
  933. {
  934.     cout<<setw(10)<<travel.BusID<<setw(10)<<travel.Origin<<setw(10)<<travel.Destination/*<<setw(10)<<travel.date*/<<setw(10)<<travel.Passengers<< endl;
  935. }
  936.  
  937.  
  938. //-----------------------------------------------------
  939. //-----------------------------------------------------
  940.  
  941. /*BuyTicketNode* searchByByBusID(int BusID,BuyTicketLinkedList &buyTicketLinkedList)
  942. {
  943.     BuyTicketNode* temp = buyTicketLinkedList.root;
  944.     while(temp != NULL)
  945.     {
  946.         if( strcmp(searchKey,temp->travel.BusID) == 0 )
  947.             return temp;
  948.         temp = temp->next;
  949.     }
  950.     return NULL;
  951. */
  952.  
  953. //---------------------------------------------------------------------------------------------------------------------
  954. //--------------passenger----------------------------------------------------------------------------------------------
  955. //---------------------------------------------------------------------------------------------------------------------
  956.  
  957.  
  958. void uiAddPassenger(BuyTicketLinkedList &buyTicketLinkedList)
  959. {
  960.     Passenger newPassenger=uiGetPassenger();
  961.     AddPassenger(newPassenger,buyTicketLinkedList);
  962. }
  963.  
  964.  
  965. //-----------------------------------------------------
  966. //-----------------------------------------------------
  967.  
  968.  
  969.  
  970. void uiSearchByTellNumber(BuyTicketLinkedList &buyTicketLinkedList)
  971. {
  972.     const int TellNumberSize = 20;
  973.     char searchKey[TellNumberSize];
  974.  
  975.     cout << "Enter the tell number of passenger: " ;
  976.     cin >> searchKey;
  977.  
  978.     BuyTicketNode* result = searchByByTellNumber(searchKey,buyTicketLinkedList);
  979.  
  980.     if( result == NULL )
  981.         cout << "No such tell number found!" << endl;
  982.     else
  983.         uiDisplay(result->travel);
  984.  
  985. }
  986.  
  987. //-----------------------------------------------------
  988. //-----------------------------------------------------
  989.  
  990.  
  991. int countPassenger(BuyTicketLinkedList &buyTicketLinkedList)
  992. {
  993.     BuyTicketNode* temp = buyTicketLinkedList.root;
  994.  
  995.     int counter = 0;
  996.  
  997.     while(temp != NULL)
  998.     {
  999.         counter++;
  1000.         temp = temp->next;
  1001.     }
  1002.     return counter;
  1003. }
  1004.  
  1005.  
  1006.  
  1007.  
  1008. //-----------------------------------------------------
  1009. //-----------------------------------------------------
  1010.  
  1011. void uiDisplayPassenger(BuyTicketLinkedList &buyTicketLinkedList)
  1012. {
  1013.     BuyTicketNode* temp = buyTicketLinkedList.root;
  1014.     if(temp == NULL)
  1015.     {
  1016.         cout << "The list is empty!" << endl;
  1017.     }
  1018.     else
  1019.     {
  1020.         cout << setw(25) << "Name" << setw(25) << "Family" << setw(25) << "Tell Number" <<setw(20)<<"Passenger ID"<<endl;
  1021.         cout << setw(25) << "----" << setw(25) << "------" << setw(25) << "----------" << setw(20)<<"------------"<<endl;
  1022.         while(temp != NULL)
  1023.         {
  1024.             uiDisplay(temp->bus);
  1025.             temp = temp->next;
  1026.         }
  1027.     }
  1028. }
  1029.  
  1030. //-----------------------------------------------------
  1031. //-----------------------------------------------------
  1032.  
  1033. Passenger uiGetPassenger()
  1034. {
  1035.     const int NameSize=20;
  1036.     const int FamilySize=20;
  1037.     const int TellNumberSize=20;
  1038.  
  1039.     Passenger passenger;
  1040.     passenger.Name=new char[NameSize];
  1041.     passenger.Family=new char[FamilySize];
  1042.     passenger.TellNumber=new char[TellNumberSize];
  1043.  
  1044.     cout<<"Enter your first name: ";
  1045.     cin>>passenger.Name;
  1046.  
  1047.     cout<<"Enter your last name: ";
  1048.     cin>>passenger.Family;
  1049.  
  1050.     cout<<"Enter your tell number: ";
  1051.     cin>>passenger.TellNumber;
  1052.  
  1053.     cout<<"Enter your ID of passenger: ";
  1054.     cin>>passenger.PassengerID;
  1055.  
  1056.     return passenger;
  1057. }
  1058.  
  1059.  
  1060.  
  1061. //-----------------------------------------------------
  1062. //-----------------------------------------------------
  1063.  
  1064. void AddPassenger(Passenger newPassenger,BuyTicketLinkedList &buyTicketLinkedList)
  1065. {
  1066.     BuyTicketNode* buyTicketNode = new BuyTicketNode;
  1067.     buyTicketNode->passenger = newPassenger;
  1068.     buyTicketNode->next = NULL;
  1069.  
  1070.     BuyTicketNode* temp = buyTicketLinkedList.root;
  1071.     if(temp == NULL)
  1072.     {
  1073.         buyTicketLinkedList.root = buyTicketNode;
  1074.     }
  1075.     else
  1076.     {
  1077.         while(temp->next != NULL)
  1078.             temp = temp->next;
  1079.         temp->next = buyTicketNode;
  1080.     }    
  1081. }
  1082.  
  1083. //-----------------------------------------------------
  1084. //-----------------------------------------------------
  1085.  
  1086. void uiDisplay(Passenger passenger)
  1087. {
  1088.     cout << setw(25) << passenger.Name << setw(25) << passenger.Family << setw(25) << passenger.TellNumber<<setw(20)<<passenger.PassengerID<< endl;
  1089. }
  1090.  
  1091.  
  1092. //-----------------------------------------------------
  1093. //-----------------------------------------------------
  1094.  
  1095.  
  1096. BuyTicketNode* searchByByTellNumber(char* searchKey,BuyTicketLinkedList &buyTicketLinkedList)
  1097. {
  1098.     BuyTicketNode* temp = buyTicketLinkedList.root;
  1099.     while(temp != NULL)
  1100.     {
  1101.         if( strcmp(searchKey,temp->passenger.TellNumber) == 0 )
  1102.             return temp;
  1103.         temp = temp->next;
  1104.     }
  1105.     return NULL;
  1106. }
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
Mar 10 '10 #1
1 5455
weaknessforcats
9,208 Expert Mod 8TB
In order to search between dates or by destinations, etc.. your lest needs to be sorted. That is, to search by date it needs to be sorted by date. To search by destination it needs to be sorted by destination.

One way to do this, say for a search by date, us to make a pass of your linked list and extract all nodes in the date range by creating a temporary linked list and adding nodes to that temprary list with the data from your main list. Now you have all the dates within your search range.

When you are finished, delete the temporary list.

You will need to do this for each of your searches so you should be able touse the same code for the temprary linked list for each search.
Mar 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
6
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h>...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
by: Sean Patterson | last post by:
Hey all, I've followed the examples online on how to use Forms Authentication to create a ticket, assign it a role, and then intercept it in the Global.asax file to make sure it gets sucked in...
7
by: spidey12345 | last post by:
can anybody help me with this i have a baddata.txt file that has certain data like this: " blah blah ere werew ss a s ef df ww erew asf" and i...
4
by: mendiratta | last post by:
Hello, I am Learning Link List. Today wrote a simple program, but not working. Program is like this #include<stdio.h> #include<stdlib.h> #include<alloca.h> struct node{ int...
9
by: ratika | last post by:
can anyone tell me how to delete a certain node in a doubly circular link list
5
by: johnnash | last post by:
i'm declaring a data structure for link list of integers in A.h #ifndef A_H (can anyone please explain how ifndef works as well..i just seem to see it in almost every program) #define A_H ...
14
Parul Bagadia
by: Parul Bagadia | last post by:
Here is the code i hav written for inserting a no., after given no. in a link list; i guess the logic is ofcourse right. there is no error in it, but at the time of display its not displaying the...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.