Advertisement
jeicamad

Untitled

Mar 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct node {
  7. int val;
  8. node* next;
  9. };
  10. void Add(node*&, double);
  11. void Show(node*);
  12. void LoadList(node*&);
  13. bool isEmpty(node*&);
  14. void Swap(node*&);
  15. void bubblesort(node*&);
  16.  
  17. int main()
  18. {
  19. node *H = NULL;
  20. LoadList(H);
  21. //Sortuj(H);
  22. bubblesort(H);
  23. Show(H);
  24. return 0;
  25. }
  26.  
  27. void Add(node*& Head, double x) {
  28. node* p = new node;
  29. p->val = x;
  30. p->next = NULL;
  31.  
  32. if (Head == NULL) {
  33. Head = p;
  34. }
  35. else {
  36. node *q = Head;
  37. while (q->next != NULL) {
  38. q = q->next;
  39. }
  40. q->next = p;
  41. }
  42. }
  43. void Show(node * H) {
  44. node* p = H;
  45. while (p != NULL) {
  46. cout << p->val << " ";
  47. p = p->next;
  48. }
  49. }
  50. void LoadList(node *&H) {
  51. double tmp;
  52. fstream plik;
  53. plik.open("G:\\Liczby.txt", std::ios::in | std::ios::out);
  54. if (plik.good() == true)
  55. {
  56. while (!plik.eof()) {
  57. plik >> tmp;
  58. Add(H, tmp);
  59. }
  60. plik.close();
  61. }
  62. }
  63.  
  64. bool isEmpty(node *&H) {
  65. return(H->next == NULL);
  66. }
  67. void Swap(node *&Head) {
  68. node* p = Head;
  69. node* q = p->next;
  70.  
  71. p->next = q->next;
  72. q->next = Head;
  73. Head = q;
  74. p = q;
  75. q = Head;
  76. }
  77. void bubblesort(node *&Head) {
  78. if (Head != NULL && Head->next != NULL) {
  79. node *p = Head;
  80. node *q = Head->next;
  81. node *PP = Head;
  82. node *zderzak = NULL;
  83. while(Head!=zderzak) {
  84. while (q != zderzak) {
  85. if (p->val > q->val) {
  86. if (p == Head) {
  87. p->next = q->next;
  88. q->next = Head;
  89. Head = q;
  90. p = q;
  91. q = p->next;
  92. }
  93. else {
  94. PP->next = q;
  95. p->next = q->next;
  96. q->next = p;
  97. q = p;
  98. p = PP->next;
  99. }
  100.  
  101. }
  102.  
  103. PP = p;
  104. PP->val;
  105. p = p->next;
  106. q = q->next;
  107. }
  108. zderzak = p;
  109. //cout << zderzak->val;
  110. p = Head;
  111. q = Head->next;
  112. PP = Head;
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement