List.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "List.h"
  2. #include "ListNode.h"
  3. #include <string>
  4. using namespace std;
  5. // constructor with 0 parameters
  6. List::List() :
  7. List("List")
  8. {
  9. }
  10. // constructor with name parameter
  11. List::List(string name) :
  12. first_node(nullptr), last_node(nullptr), name(name)
  13. {
  14. }
  15. List::~List()
  16. {
  17. // set current to the start of the list
  18. ListNode * current = first_node;
  19. ListNode * to_delete;
  20. // clean the list until the end
  21. while(current != nullptr)
  22. {
  23. to_delete = current;
  24. // set next to traverse through the list
  25. current = current->get_next();
  26. // delete the data in the node
  27. delete to_delete;
  28. }
  29. }
  30. // insert at front of list
  31. void List::insert_at_front(void * data)
  32. {
  33. // cases
  34. // 1) empty list
  35. // 2) list with many elements
  36. // case 1
  37. if(first_node == nullptr)
  38. {
  39. // set first node to new peice of data
  40. first_node = new ListNode(data, nullptr);
  41. // set last node to the new data since there is only one element
  42. last_node = first_node;
  43. }
  44. // case 2
  45. else
  46. {
  47. // set next of new first node to current first node
  48. first_node = new ListNode(data, first_node);
  49. }
  50. }
  51. // insert at back of list
  52. void List::insert_at_back(void * data)
  53. {
  54. // cases
  55. // 1) empty list
  56. // 2) list with many elements
  57. // case 1
  58. if(first_node == nullptr)
  59. {
  60. // set last node to the new data
  61. last_node = new ListNode(data, nullptr);
  62. // set the first node to the last node since there is only one element
  63. first_node = last_node;
  64. }
  65. // case 2
  66. else
  67. {
  68. // next of last node is 0
  69. ListNode * new_node = new ListNode(data, nullptr);
  70. // set current last node to the new instance
  71. last_node->set_next(new_node);
  72. // set last node to the new node
  73. last_node = new_node;
  74. }
  75. }
  76. // remove from front of list
  77. void * List::remove_from_front(void)
  78. {
  79. void * removed_data = nullptr;
  80. // cases
  81. // 1) empty list
  82. // 2) one element in list
  83. // 3) multiple elements in list
  84. // empty list
  85. if(first_node == nullptr)
  86. {
  87. removed_data = nullptr;
  88. }
  89. // one element in list
  90. else if(first_node == last_node)
  91. {
  92. // removed_data holds the address of the first element
  93. removed_data = first_node;
  94. // null first and last node no more elements
  95. first_node = nullptr;
  96. last_node = nullptr;
  97. }
  98. // multiple element in list
  99. else
  100. {
  101. // remvoed data holds the address of the first element
  102. removed_data = first_node;
  103. // set the 2nd element in the list as the first
  104. first_node = first_node->get_next();
  105. }
  106. return removed_data;
  107. }
  108. // remove from back of list
  109. void * List::remove_from_back(void)
  110. {
  111. // cases
  112. // 1) empty list
  113. // 2) one element in list
  114. // 3) multiple elements in list
  115. void * removed_data = nullptr;
  116. // empty list
  117. if(first_node == nullptr)
  118. {
  119. removed_data = nullptr;
  120. }
  121. // one element in list
  122. else if(first_node == last_node)
  123. {
  124. // removed data holds the address of the last element
  125. removed_data = last_node;
  126. // set the last node to null as list is empty
  127. first_node = nullptr;
  128. // set the last node to null as list is empty
  129. last_node = nullptr;
  130. }
  131. // multiple elements in list
  132. else
  133. {
  134. ListNode * current = first_node;
  135. while(current->get_next() != last_node)
  136. {
  137. current = current->get_next();
  138. }
  139. // removed data holds the address of the current last element in the
  140. // list
  141. removed_data = last_node;
  142. // set the last node to equal to the current variable which is the
  143. // second to last element in the list
  144. last_node = current;
  145. }
  146. return removed_data;
  147. }
  148. // get the first node in the list
  149. ListNode * List::get_first(void)
  150. {
  151. return first_node;
  152. }