fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class ArrivalList
  7. {
  8. private:
  9. struct Arrival
  10. {
  11. string arrAddress;
  12. double distance;
  13. string roadCategory;
  14. Arrival* next;
  15. };
  16.  
  17. Arrival* head;
  18.  
  19. ArrivalList(const ArrivalList&) {}
  20. ArrivalList& operator=(const ArrivalList&) { return *this; }
  21.  
  22. public:
  23.  
  24. ArrivalList();
  25. ~ArrivalList();
  26.  
  27. bool addArrival(string oneAddress, double distance, string roadCategory);
  28.  
  29. void print() const;
  30. };
  31.  
  32. ArrivalList::ArrivalList() : head(NULL) {}
  33.  
  34. ArrivalList::~ArrivalList()
  35. {
  36. Arrival* current = head;
  37. while (current)
  38. {
  39. Arrival* temp = current->next;
  40. delete current;
  41. current = temp;
  42. }
  43. }
  44.  
  45. bool ArrivalList::addArrival(string oneAddress, double distance, string roadCategory)
  46. {
  47. Arrival** current = &head;
  48.  
  49. while ((*current != NULL) && (oneAddress >= (*current)->arrAddress))
  50. current = &((*current)->next);
  51.  
  52. Arrival* temp = new Arrival;
  53. temp->arrAddress = oneAddress;
  54. temp->distance = distance;
  55. temp->roadCategory = roadCategory;
  56. temp->next = *current;
  57.  
  58. *current = temp;
  59.  
  60. return true;
  61. }
  62.  
  63. void ArrivalList::print() const
  64. {
  65. if (head)
  66. {
  67. Arrival* current = head;
  68. while (current)
  69. {
  70. cout << current->arrAddress << ' ';
  71. current = current->next;
  72. }
  73. cout << endl;
  74. }
  75. else
  76. {
  77. cout << "(empty)" << endl;
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. ArrivalList li;
  84. li.print();
  85.  
  86. li.addArrival("jjjjj",0.8999,"I");
  87. li.print();
  88.  
  89. li.addArrival("aaaaa",0.888,"k");
  90. li.print();
  91.  
  92. li.addArrival("lllll",0.888,"k");
  93. li.print();
  94.  
  95. li.addArrival("kkkkk",0.888,"k");
  96. li.print();
  97. }
  98.  
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
(empty)
jjjjj 
aaaaa jjjjj 
aaaaa jjjjj lllll 
aaaaa jjjjj kkkkk lllll