fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class ListItem
  6. {
  7. public:
  8. string Data;
  9. ListItem *Next;
  10. };
  11.  
  12. class List
  13. {
  14. private:
  15. ListItem *root = nullptr;
  16. ListItem *last = nullptr;
  17.  
  18. public:
  19. ~List();
  20. void Anhaengen(string data);
  21. void Ausgabe();
  22. int GetCount();
  23.  
  24. };
  25.  
  26. List::~List()
  27. {
  28. ListItem *helper;
  29. while (this->root != nullptr)
  30. {
  31. helper = this->root;
  32. this->root = this->root->Next;
  33. delete(helper);
  34. }
  35. }
  36.  
  37. void List::Anhaengen(string data)
  38. {
  39. ListItem *item = new(ListItem);
  40. item->Data = data;
  41. item->Next = nullptr;
  42. if(this->root == nullptr)
  43. {
  44. this->root = item;
  45. this->last = item;
  46. }
  47. else
  48. {
  49. this->last->Next = item;
  50. this->last = item;
  51. }
  52. }
  53.  
  54. void List::Ausgabe()
  55. {
  56. ListItem *helper = this->root;
  57.  
  58. while (helper != nullptr)
  59. {
  60. cout << helper->Data << endl;
  61. helper = helper->Next;
  62. }
  63. }
  64.  
  65. int List::GetCount()
  66. {
  67. int count = 0;
  68. ListItem *helper = this->root;
  69.  
  70. while (helper != nullptr)
  71. {
  72. helper = helper->Next;
  73. count++;
  74. }
  75.  
  76. return count;
  77. }
  78.  
  79. int main() {
  80.  
  81. List list;
  82.  
  83. for(int i = 1; i < 11; i++)
  84. list.Anhaengen("Test" + std::to_string(i));
  85.  
  86. list.Ausgabe();
  87. cout << "Items: " << list.GetCount() << endl;
  88.  
  89. // your code goes here
  90. return 0;
  91. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Test1
Test2
Test3
Test4
Test5
Test6
Test7
Test8
Test9
Test10
Items: 10