List.cpp 3.9 KB

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