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