Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef DEBUG
  5. #define DEBUG(...) printf(__VA_ARGS__)
  6. #endif
  7.  
  8. #define MAX_SIZE 10
  9. typedef struct Node{
  10. struct Node *next;
  11. int val;
  12. } node;
  13.  
  14. int add_node(node **head, int val) {
  15. node *new;
  16.  
  17. new = (node *)malloc(sizeof(node));
  18. if (!new) return -1;
  19.  
  20. new->val = val;
  21. new->next = *head;
  22. *head = new;
  23.  
  24. return 0;
  25. }
  26.  
  27. void print_list(node *head) {
  28. node *trenutni;
  29.  
  30. if(!head) return;
  31.  
  32. for(trenutni=head; trenutni!=NULL; trenutni=trenutni->next) printf("%d ", trenutni->val);
  33. printf("\n");
  34. }
  35.  
  36. void bubble_sort(node **head) {
  37. int counter = 0;
  38. int n = 10;
  39. int brojswaps;
  40. node *trenutni = (*head)->next;
  41. node *prethodni;
  42. node *temp;
  43.  
  44. while(n > 0){
  45. temp = *head;
  46. prethodni = *head;
  47. trenutni = (*head)->next;
  48. counter = 0;
  49. brojswaps = 0;
  50. //for(trenutni = (*head)->next; trenutni != NULL; trenutni = trenutni->next){
  51. while(trenutni != NULL){
  52. //uvjet di moram namjestit head
  53. //ova 2 if su uvjeti u slucajevima gdje se desava zamjena
  54. counter++;
  55. //printf("t = %d, head = %d, prethod = %d, tmp=%d\n", trenutni->val, (*head)->val, prethodni->val,temp->val);
  56. if(prethodni->val > trenutni->val && prethodni == *head){
  57. prethodni->next = trenutni->next;
  58. trenutni->next = prethodni;
  59. *head = trenutni;
  60. //ovisno ako se mjenjo head mjenjo se i temp u tom trenutku
  61. temp = trenutni;
  62. trenutni = prethodni->next;
  63. brojswaps++;
  64. continue;
  65. }else if(prethodni->val > trenutni->val && prethodni != *head){
  66. //printf("nh t = %d, prethodn = %d\n", trenutni->val, prethodni->val);
  67. prethodni->next = trenutni->next;
  68.  
  69. //u slucaju neke zamjene temp->next je trenutni
  70. //ali i pomakni sad tmp na taj novi
  71. temp->next = trenutni;
  72. temp = temp->next;
  73.  
  74. trenutni->next = prethodni;
  75. trenutni = prethodni->next;
  76. brojswaps++;
  77. continue;
  78. }
  79.  
  80. //ako nema zamjene
  81. //printf("nema zam t = %d, prethodn = %d\n", trenutni->val, prethodni->val);
  82. prethodni = prethodni->next;
  83. trenutni = trenutni->next;
  84. if(counter > 1) temp = temp->next;
  85. //printf("nema zam t = %d, prethodn = %d\n", trenutni->val, prethodni->val);
  86. }
  87. //if(brojswaps == 0) break;
  88. print_list(*head);
  89. //printf("broj swap = %d\n", brojswaps);
  90. if(brojswaps == 0) break;
  91. n--;
  92. }
  93. }
  94.  
  95. int main() {
  96. node *head = NULL;
  97. int i, val;
  98.  
  99. for(i=0; i<MAX_SIZE; i++) {
  100. scanf("%d", &val);
  101. add_node(&head, val);
  102. }
  103.  
  104. printf("\nLista prije sortiranja:\n");
  105. print_list(head);
  106.  
  107. printf("\nLista za vrijeme sortiranja:\n");
  108. bubble_sort(&head);
  109.  
  110. printf("\nSortirana lista:\n");
  111. print_list(head);
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement