Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. /**
  2. * @file list.cpp
  3. * Doubly Linked List (MP 3).
  4. */
  5. template <class T>
  6. List<T>::List() {
  7. // @TODO: graded in MP3.1
  8. head_ = NULL;
  9. tail_ = NULL;
  10. length_=0;
  11. }
  12.  
  13. /**
  14. * Returns a ListIterator with a position at the beginning of
  15. * the List.
  16. */
  17. template <typename T>
  18. typename List<T>::ListIterator List<T>::begin() const {
  19. // @TODO: graded in MP3.1
  20. return List<T>::ListIterator(head_);
  21. }
  22.  
  23. /**
  24. * Returns a ListIterator one past the end of the List.
  25. */
  26. template <typename T>
  27. typename List<T>::ListIterator List<T>::end() const {
  28. // @TODO: graded in MP3.1
  29. return List<T>::ListIterator(NULL);
  30. }
  31.  
  32.  
  33. /**
  34. * Destroys all dynamically allocated memory associated with the current
  35. * List class.
  36. */
  37. template <typename T>
  38. void List<T>::_destroy() {
  39. /// @todo Graded in MP3.1
  40. ListNode * temp = head_;
  41. if(temp==NULL)
  42. {
  43. return;
  44. }
  45. ListNode * temp2;
  46. while(temp!=NULL)
  47. {
  48. temp2=temp->next;
  49. delete temp;
  50. temp = temp2;
  51. }
  52. //return;
  53. }
  54.  
  55. /**
  56. * Inserts a new node at the front of the List.
  57. * This function **SHOULD** create a new ListNode.
  58. *
  59. * @param ndata The data to be inserted.
  60. */
  61. template <typename T>
  62. void List<T>::insertFront(T const & ndata) {
  63. //std::cout<<"pass"<<std::endl;
  64. /// @todo Graded in MP3.1
  65. ListNode * newNode = new ListNode(ndata);
  66. newNode -> next = head_;
  67. newNode -> prev = NULL;
  68.  
  69. if (head_ != NULL) {
  70. head_ ->prev = newNode;
  71. }
  72. if (tail_ == NULL) {
  73. tail_ = newNode;
  74. }
  75.  
  76. head_=newNode;
  77. length_++;
  78.  
  79. }
  80.  
  81. /**
  82. * Inserts a new node at the back of the List.
  83. * This function **SHOULD** create a new ListNode.
  84. *
  85. * @param ndata The data to be inserted.
  86. */
  87. template <typename T>
  88. void List<T>::insertBack(const T & ndata) {
  89. /// @todo Graded in MP3.1
  90. ListNode * newNode = new ListNode(ndata);
  91. newNode -> next = NULL;
  92. newNode -> prev = tail_;
  93. if (head_ == NULL) {
  94. head_ = newNode;
  95. }
  96. if (tail_ != NULL) {
  97. tail_ -> next = newNode;
  98. }
  99.  
  100. tail_=newNode;
  101. length_++;
  102. }
  103.  
  104. /**
  105. * Helper function to split a sequence of linked memory at the node
  106. * splitPoint steps **after** start. In other words, it should disconnect
  107. * the sequence of linked memory after the given number of nodes, and
  108. * return a pointer to the starting node of the new sequence of linked
  109. * memory.
  110. *
  111. * This function **SHOULD NOT** create **ANY** new List or ListNode objects!
  112. *
  113. * This function is also called by the public split() function located in
  114. * List-given.hpp
  115. *
  116. * @param start The node to start from.
  117. * @param splitPoint The number of steps to walk before splitting.
  118. * @return The starting node of the sequence that was split off.
  119. */
  120. template <typename T>
  121. typename List<T>::ListNode * List<T>::split(ListNode * start, int splitPoint) {
  122. /// @todo Graded in MP3.1
  123. if(start==NULL)
  124. {
  125. return NULL;
  126. }
  127. if(splitPoint<1)
  128. {
  129. return start;
  130. }
  131. if(length_<splitPoint)
  132. {
  133. NULL;
  134. }
  135. ListNode * curr = start;
  136.  
  137. for (int i = 0; i < splitPoint-1 && curr != NULL; i++) {
  138. if(curr->next!=NULL)
  139. curr = curr->next;
  140. }
  141.  
  142. if (curr != NULL) {
  143. curr=curr->next;
  144. curr->prev->next = NULL;
  145. curr->prev = NULL;
  146. tail_=curr->prev;
  147. return curr;
  148. }
  149.  
  150. return NULL;
  151. }
  152.  
  153. /**
  154. * Modifies the List using the waterfall algorithm.
  155. * Every other node (starting from the second one) is removed from the
  156. * List, but appended at the back, becoming the new tail. This continues
  157. * until the next thing to be removed is either the tail (**not necessarily
  158. * the original tail!**) or NULL. You may **NOT** allocate new ListNodes.
  159. * Note that since the tail should be continuously updated, some nodes will
  160. * be moved more than once.
  161. */
  162. template <typename T>
  163. void List<T>::waterfall() {
  164. /// @todo Graded in MP3.1
  165. if(head_==NULL ||head_->next==NULL)
  166. {
  167. return;
  168. }
  169. ListNode * temp = head_;
  170. ListNode * temp2 = temp->next;
  171. int a = 0;
  172. while(temp->next->next!=NULL && temp->next!=NULL)
  173. {
  174. temp=temp->next;
  175. temp->prev->next=temp->next;
  176. temp->next->prev =temp->prev;
  177. temp2 = temp->next;
  178. tail_->next = temp;
  179. temp->prev = tail_;
  180. temp->next = NULL;
  181. tail_ = temp;
  182. temp= temp2;
  183.  
  184.  
  185. }
  186.  
  187.  
  188. }
  189.  
  190. /**
  191. * Reverses the current List.
  192. */
  193. template <typename T>
  194. void List<T>::reverse() {
  195. reverse(head_, tail_);
  196. }
  197.  
  198. /**
  199. * Helper function to reverse a sequence of linked memory inside a List,
  200. * starting at startPoint and ending at endPoint. You are responsible for
  201. * updating startPoint and endPoint to point to the new starting and ending
  202. * points of the rearranged sequence of linked memory in question.
  203. *
  204. * @param startPoint A pointer reference to the first node in the sequence
  205. * to be reversed.
  206. * @param endPoint A pointer reference to the last node in the sequence to
  207. * be reversed.
  208. */
  209. template <typename T>
  210. void List<T>::reverse(ListNode *& startPoint, ListNode *& endPoint) {
  211. /// @todo Graded in MP3.2
  212. }
  213.  
  214. /**
  215. * Reverses blocks of size n in the current List. You should use your
  216. * reverse( ListNode * &, ListNode * & ) helper function in this method!
  217. *
  218. * @param n The size of the blocks in the List to be reversed.
  219. */
  220. template <typename T>
  221. void List<T>::reverseNth(int n) {
  222. /// @todo Graded in MP3.2
  223. }
  224.  
  225.  
  226. /**
  227. * Merges the given sorted list into the current sorted list.
  228. *
  229. * @param otherList List to be merged into the current list.
  230. */
  231. template <typename T>
  232. void List<T>::mergeWith(List<T> & otherList) {
  233. // set up the current list
  234. head_ = merge(head_, otherList.head_);
  235. tail_ = head_;
  236.  
  237. // make sure there is a node in the new list
  238. if (tail_ != NULL) {
  239. while (tail_->next != NULL)
  240. tail_ = tail_->next;
  241. }
  242. length_ = length_ + otherList.length_;
  243.  
  244. // empty out the parameter list
  245. otherList.head_ = NULL;
  246. otherList.tail_ = NULL;
  247. otherList.length_ = 0;
  248. }
  249.  
  250. /**
  251. * Helper function to merge two **sorted** and **independent** sequences of
  252. * linked memory. The result should be a single sequence that is itself
  253. * sorted.
  254. *
  255. * This function **SHOULD NOT** create **ANY** new List objects.
  256. *
  257. * @param first The starting node of the first sequence.
  258. * @param second The starting node of the second sequence.
  259. * @return The starting node of the resulting, sorted sequence.
  260. */
  261. template <typename T>
  262. typename List<T>::ListNode * List<T>::merge(ListNode * first, ListNode* second) {
  263. /// @todo Graded in MP3.2
  264. return NULL;
  265. }
  266.  
  267. /**
  268. * Sorts a chain of linked memory given a start node and a size.
  269. * This is the recursive helper for the Mergesort algorithm (i.e., this is
  270. * the divide-and-conquer step).
  271. *
  272. * Called by the public sort function in List-given.hpp
  273. *
  274. * @param start Starting point of the chain.
  275. * @param chainLength Size of the chain to be sorted.
  276. * @return A pointer to the beginning of the now sorted chain.
  277. */
  278. template <typename T>
  279. typename List<T>::ListNode* List<T>::mergesort(ListNode * start, int chainLength) {
  280. /// @todo Graded in MP3.2
  281. return NULL;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement