Advertisement
Guest User

Linked List Insertion

a guest
Apr 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /* Insert Name(s) here */
  2.  
  3. /* Answer the following quesions in the comments Before starting to progam:
  4.  
  5. What is the purpose of the following pointers: (look at the code from the book)
  6.  
  7. head
  8. add
  9. add->next
  10. insert
  11. */
  12.  
  13.  
  14. #include<iostream>
  15. using namespace std;
  16.  
  17. /* Using some code from the book - see Chapter 19 Programs Number List */
  18. class NumberList
  19. {
  20. private:
  21.  
  22. struct ListNode
  23. {
  24. double value;
  25. struct ListNode *next;
  26. };
  27.  
  28. ListNode *head;
  29.  
  30. public:
  31. /* Notice that this constructor is different than the one in the book.
  32. * When a new linked list is made, a head node (which is given the value 0)
  33. * is created and the head pointer will point to it. This head node should not be
  34. * considered part of the data, it is just there to make the sorting easier.
  35. */
  36.  
  37. NumberList()
  38. {
  39. head = new ListNode;
  40. head->value = 0;
  41. head->next = NULL;
  42. }
  43.  
  44.  
  45. void appendNode(double);
  46. void InsertionSort();
  47. void displayList() const;
  48. };
  49.  
  50. void NumberList::appendNode(double num)
  51. {
  52. /* Just copy and paste the code for this function from the book.
  53. * Don't add anything new
  54. */
  55. }
  56.  
  57.  
  58. void NumberList::displayList() const
  59. {
  60. /* Just copy and paste the code for this function from the book.
  61. * Don't add anything new
  62. */
  63. }
  64.  
  65. void NumberList::InsertionSort()
  66. {
  67.  
  68. /* You can only use the 4 variables listed below. Dont' create any other variables*/
  69. /* Don't add any more code than specified by the comments */
  70.  
  71. NumberList temp;
  72. ListNode *add;
  73.  
  74. ListNode *addNext;
  75. ListNode *insert;
  76.  
  77. add = head->next; //The first node to be sorted is the one after the head node (which has the value 0)
  78.  
  79. while (/*stop when there is nothing left to sort in the original list*/)
  80. {
  81. /* Replace this comment with one line of code to assign a value to addNext */
  82.  
  83. /* Replace this comment with one line of code to assign a value to insert */
  84.  
  85. while( /*stop when you are at the end of the temp list*/)
  86. {
  87. if(/* use the > operator to determine when to break out of this inner while loop*/)
  88. {
  89. break;
  90. }
  91. /* Replace this comment with one line of code to assign a value to insert */
  92. }
  93. /* Replace this comment with one line of code to assign a value to add->next */
  94.  
  95. /* Replace this comment with one line of code to assign a value to insert->next */
  96.  
  97. /* Replace this comment with one line of code to assign a value to add */
  98.  
  99. }
  100.  
  101. delete head;
  102.  
  103. /* Replace this comment with one line of code to assign a value to head */
  104.  
  105. temp.head = NULL;
  106.  
  107. }
  108.  
  109.  
  110. int main()
  111. {
  112. NumberList example;
  113. example.appendNode(5);
  114. example.appendNode(2);
  115. example.appendNode(4);
  116. example.appendNode(7);
  117. example.appendNode(3);
  118. example.appendNode(1);
  119. example.appendNode(6);
  120.  
  121. cout <<"In original order: "<<endl;
  122. example.displayList();
  123. cout <<"\n\nIn sorted order: "<<endl;
  124. example.InsertionSort();
  125. example.displayList();
  126.  
  127. system ("pause");
  128. return 0;
  129.  
  130. }
  131. /* Sample Output In original order:
  132. 0
  133. 5
  134. 2
  135. 4
  136. 7
  137. 3
  138. 1
  139. 6
  140. In sorted order:
  141. 0
  142. 1
  143. 2
  144. 3
  145. 4
  146. 5
  147. 6
  148. 7
  149. Press any key to continue . . .
  150. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement