fork(1) download
  1. #include <stdio.h>
  2.  
  3. struct node {
  4. char data[3];
  5. struct node *next;
  6. };
  7.  
  8. void swap(struct node **p0_next) {
  9. struct node *p1 = *p0_next;
  10. struct node *p2 = p1->next;
  11. struct node *tmp = *p0_next;
  12.  
  13. *p0_next = p1->next;
  14. p1->next = p2->next;
  15. p2->next = tmp;
  16. }
  17.  
  18. void print_list(struct node *cursor) {
  19. for (; cursor != NULL; cursor = cursor->next) {
  20. printf("[%s] -> ", cursor->data);
  21. if (cursor->next == NULL) {
  22. printf("[NULL]\n");
  23. }
  24. }
  25. }
  26.  
  27. int main(void) {
  28. struct node list[5] = {
  29. { "p0", list + 1 },
  30. { "p1", list + 2 },
  31. { "p2", list + 3 },
  32. { "p3", NULL }
  33. };
  34.  
  35. print_list(list);
  36. swap(&list[0].next);
  37. print_list(list);
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[p0] -> [p1] -> [p2] -> [p3] -> [NULL]
[p0] -> [p2] -> [p1] -> [p3] -> [NULL]