Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int num;
  6. int occ;
  7. struct node *next;
  8. struct node *prev;
  9. };
  10.  
  11. void printlist(struct node *list_head){
  12. struct node *iteratore = list_head;
  13. while (iteratore != NULL){
  14. printf("{num: %d, occ: %d}->", iteratore->num, iteratore->occ);
  15. iteratore = iteratore->next;
  16. }
  17. printf("\b\b \n");
  18. return;
  19. }
  20.  
  21. struct node *createnode(int x){
  22. struct node *node = (struct node*)malloc(sizeof(struct node));
  23. node->num = x;
  24. node->occ = 1;
  25. return node;
  26. }
  27.  
  28. void insertnode(struct node **list_head, int x){
  29.  
  30. // inserimento in testa
  31. if (*list_head == NULL){
  32. struct node *node = createnode(x);
  33. node->next = NULL;
  34. node->prev = NULL;
  35. *list_head = node;
  36. return;
  37. }
  38.  
  39. // inserimento in lista
  40. struct node *iteratore = *list_head;
  41. struct node *node = createnode(x);
  42. while(iteratore->next != NULL){
  43. if (x == iteratore->num){
  44. iteratore->occ +=1;
  45. node->occ = iteratore->occ;
  46. }
  47. iteratore = iteratore->next;
  48. }
  49. node->next = NULL;
  50. node->prev = iteratore;
  51. iteratore->next = node;
  52. return;
  53. }
  54.  
  55. struct node *list(int n, int A[n]){
  56. struct node *list_head = NULL;
  57.  
  58. int i;
  59. for (i = 0; i < n; i++){
  60. insertnode(&list_head, A[i]);
  61. }
  62.  
  63. return list_head;
  64. }
  65.  
  66. void reverse(struct node **list_head){
  67.  
  68. if (*list_head == NULL){
  69. printf ("Lista vuota!");
  70. return;
  71. }
  72.  
  73. struct node *iteratore = *list_head;
  74. while (iteratore->next != NULL){
  75. struct node *temp = iteratore->next;
  76.  
  77. // reversing list head
  78. if (iteratore == *list_head){
  79. iteratore->prev = iteratore->next;
  80. iteratore->next = NULL;
  81. }
  82. else{
  83. struct node *app = iteratore->prev;
  84. iteratore->prev = iteratore->next;
  85. iteratore->next = app;
  86. }
  87. iteratore = iteratore->prev;
  88.  
  89. }
  90.  
  91. iteratore->next = iteratore->prev;
  92. iteratore->prev = NULL;
  93. *list_head = iteratore;
  94. return;
  95. }
  96.  
  97. int main(void){
  98.  
  99. int n = 7;
  100. int array[7] = {2,7,8,2,1,5,2};
  101.  
  102. struct node *list_head;
  103. list_head = list(n, array);
  104. printlist(list_head);
  105.  
  106. reverse(&list_head);
  107. printlist(list_head);
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement