fork download
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node2
  5. {
  6. int number;
  7. struct node2 *next, *prev;
  8. };
  9.  
  10. void addNodeDouble(struct node2 **head, struct node2 **tail, int num, int thesi)
  11. {
  12. if (*head == NULL)
  13. {
  14. struct node2 * current;
  15. current = (struct node2*) malloc (1*sizeof(struct node2));
  16. current -> number = num;
  17. current -> prev = NULL;
  18. current -> next = NULL;
  19. *head = current;
  20. *tail = current;
  21. }
  22. else
  23. {
  24. if(thesi==1)
  25. {
  26. struct node2 *current,*temp;
  27. current = (struct node2*) malloc (1*sizeof(struct node2));
  28. current->number=num;
  29. temp = *head;
  30. while (temp -> next != NULL)
  31. temp = temp -> next;
  32.  
  33. temp -> next = current;
  34. current -> prev = *tail;
  35. current -> next = NULL;
  36. (*tail) -> next = current;
  37. *tail = current;
  38. }
  39. else
  40. {
  41. struct node2 *current;
  42. current = (struct node2*) malloc (1*sizeof(struct node2));
  43. current -> number = num;
  44. current -> next = *head;
  45. (*head) -> prev = current;
  46. *head = current;
  47. }
  48. }
  49. }
  50.  
  51. void ReversedisplayList(struct node2 **head, struct node2 **tail)
  52. {
  53. struct node2 *current;
  54. if(*head == NULL)
  55. printf("I lista einai adeia!\n");
  56.  
  57. else
  58. {
  59. current = *tail;
  60. while(current != NULL)
  61. {
  62. printf("%d ",current -> number);
  63. current = current -> prev;
  64. }
  65. }
  66. }
  67.  
  68. void swapElements2(struct node2 **head, struct node2 **tail)
  69. {
  70. struct node2 *current, *temp;
  71.  
  72. temp = (*tail) -> prev;
  73. current = *tail;
  74.  
  75. temp -> next = *head;
  76. current -> next = (*head) -> next;
  77. (*head) -> next = NULL;
  78. *head = current;
  79. }
  80. int main()
  81. {
  82. struct node2 *head, *tail;
  83. head = tail = NULL;
  84.  
  85. addNodeDouble(&head,&tail,4,1);
  86. addNodeDouble(&head,&tail,8,1);
  87. addNodeDouble(&head,&tail,3,0);
  88. addNodeDouble(&head,&tail,1,1);
  89. addNodeDouble(&head,&tail,7,0);
  90.  
  91. printf("\n\nDoubly linked list (reversed): ");
  92. ReversedisplayList(&head,&tail);
  93.  
  94. swapElements2(&head,&tail);
  95. printf("\nChanged list: ");
  96. ReversedisplayList(&head,&tail);
  97. }
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout

Doubly linked list (reversed): 1 8 4 3 7 
Changed list: 1 8 4 3 7