fork download
  1. #include <stdio.h>
  2. struct node {
  3. int value;
  4. struct node *next;
  5. };
  6. void rearrange(struct node *list) {
  7. struct node *p, *q;
  8. int temp;
  9. printf("%d\n",!list);
  10. printf("%p\n",list);
  11. printf("%p\n",list->next);
  12. printf("%d\n",!list->next);
  13. if (!list || !list -> next) return;
  14. p = list; q = list -> next;
  15. while(q) {
  16. temp = p -> value; p->value = q -> value;
  17. q->value = temp; p = q ->next;
  18. q = p? p ->next : 0;
  19. }
  20. printf("%p\n",p);
  21. printf("%p\n",q);
  22. }
  23.  
  24. int main(void) {
  25. // your code goes here
  26. struct node n1[7];
  27. int i;
  28. for(i=0;i<7;i++)
  29. {
  30. n1[i].value = i+1;
  31. if(i==6)
  32. n1[i].next = 0;
  33. else
  34. n1[i].next = &n1[i+1];
  35. }
  36.  
  37. rearrange(&n1);
  38.  
  39. for(i=0;i<7;i++){
  40. printf("\n%d",n1[i].value);
  41. }
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
0
0xbfac03e8
0xbfac03f0
0
0xbfac0418
(nil)

2
1
4
3
6
5
7