Advertisement
Guest User

Insertion Sort

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