Guest User

Untitled

a guest
Nov 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. // ----------------------------------------------------
  4. class Node {
  5. private:
  6. int value;
  7. Node *next;
  8. public:
  9. Node(int v) { value = v; next = NULL; }
  10. void Add(int v);
  11. void Print();
  12. void SortValues();
  13. void SortNodes();
  14. };
  15. // ----------------------------------------------------
  16. void Node::Add(int v)
  17. {
  18. Node *newItem = new Node(v);
  19. Node *temp = this;
  20. while (temp->next != NULL)
  21. temp = temp->next;
  22. temp->next = newItem;
  23. }
  24. void Node::Print()
  25. {
  26. Node *temp = this;
  27. while (temp != NULL)
  28. {
  29. cout << " " << temp << " : " << temp->value << endl;
  30. temp = temp->next;
  31. }
  32. }
  33. void Node::SortValues()
  34. {
  35. Node *swapCandidate = next;
  36. Node *current;
  37. Node *min;
  38. // L* temp;
  39. // Look at every value
  40. while (swapCandidate != NULL)
  41. {
  42. current = swapCandidate;
  43. min = swapCandidate;
  44. // ------------------------------
  45. while (current != NULL)
  46. {
  47. if (current->value < min->value)
  48. {
  49. min = current;
  50. }
  51. current = current->next;
  52. }
  53. if(min != swapCandidate)
  54. {
  55. int tempV = min->value;
  56. min->value = swapCandidate->value;
  57. swapCandidate->value = tempV;
  58. }
  59. // ------------------------------
  60. swapCandidate = swapCandidate->next;
  61. }
  62. }
  63. // ----------------------------------------------------
  64. void main()
  65. {
  66. // Sort - moving values
  67. Node *myList = new Node(0);
  68. myList->Add(15);
  69. myList->Add(3);
  70. myList->Add(20);
  71. myList->Add(1);
  72. myList->Print();
  73. myList->SortValues();
  74. cout << "------------------------------" << endl;
  75. myList->Print();
  76. system("pause");
  77. }
  78.  
  79. class LinkedList
  80. {
  81. public:
  82. // user functions should go here
  83. private:
  84. struct Node
  85. {
  86. int value;
  87. Node* next{ nullptr };
  88. }
  89. }
Add Comment
Please, Sign In to add comment