fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct Node{
  5. int data;
  6. struct Node *next;
  7. }Node;
  8.  
  9. void print(Node *lista, int pos, int length)
  10. {
  11. static int count=0;
  12. int c,half=length/2;
  13. Node *temp=lista;
  14. if(count>=length)
  15. return;
  16. count++;
  17. for(c=2;c<=pos;c++)
  18. temp=temp->next;
  19. printf("%d\t\n",temp->data);
  20. if(pos==(half+1))
  21. {
  22. if(count<=half && length%2==0)
  23. {
  24. //printf("1. pos: %d\tlength: %d\n", pos-1, length);
  25. print(lista,pos-1,length);
  26. }
  27. else
  28. {
  29. //printf("1. pos: %d\tlength: %d\n", half-1, length);
  30. print(lista,half-1,length);
  31. }
  32. }
  33. else if((pos==half) && (length%2==0))
  34. {
  35. //printf("2i). pos: %d\tlength: %d\n", pos+1, length);
  36. print(lista,pos+1,length);
  37. }
  38. else if((pos==half-1) && (length%2==0))
  39. {
  40. if(count>half)
  41. {
  42. //printf("1. pos: %d\tlength: %d\n", half+2+(half-pos), length);
  43. print(lista,half+2+(half-pos),length);
  44. }
  45. else
  46. {
  47. //printf("2. pos: %d\tlength: %d\n", pos+1, length);
  48. print(lista,pos+1,length);
  49. }
  50. }
  51. else if(pos>half)
  52. {
  53. if(count>half)
  54. {
  55. //printf("1. pos: %d\tlength: %d\n", half-(length-pos)-1, length);
  56. print(lista,half-(length-pos)-1,length);
  57. }
  58. else
  59. {
  60. //printf("3. pos: %d\tlength: %d\n", 2+(length-pos), length);
  61. print(lista,2+(length-pos),length);
  62. }
  63. }
  64. else if((pos<half))
  65. {
  66. if(count>half)
  67. {
  68. //printf("1. pos: %d\tlength: %d\n", half+2+(half-pos), length);
  69. print(lista,half+2+(half-pos),length);
  70. }
  71. else
  72. {
  73. //printf("4. pos: %d\tlength: %d\n", length-pos, length);
  74. print(lista,length-pos,length);
  75. }
  76. }
  77. }
  78. // 1 2 3 4 5 6 7 8 9
  79. // 1 8 3 6 5 4 7 2 9
  80.  
  81. // 1 2 3 4 5 6
  82. // 1 5 3 4 2 6
  83.  
  84.  
  85. // 1 2 3 4 5 6 7 8 9 10
  86. // 1 9 3 7 5 6 4 8 2 10
  87. Node *insert(Node *lista, int data)
  88. {
  89. Node *n1, *temp;
  90. n1=(Node *)malloc(sizeof(Node));
  91. n1->data=data;
  92. n1->next=NULL;
  93. if(lista==NULL)
  94. {
  95. lista=n1;
  96. return lista;
  97. }
  98. temp=lista;
  99. for(;temp->next;temp=temp->next);
  100. temp->next=n1;
  101. return lista;
  102. }
  103.  
  104. void disp(Node *lista)
  105. {
  106. for(;lista;lista=lista->next)
  107. printf("%d-->",lista->data);
  108. }
  109.  
  110. int main()
  111. {
  112. Node *lista=NULL;
  113. int i,n;
  114. scanf("%d",&n);
  115. for(i=1;i<=n;i++)
  116. lista=insert(lista,i);
  117. disp(lista);
  118. printf("\n");
  119. //printf("%d\n", n/2);
  120. print(lista,1,n);
  121. return 0;
  122. }
  123.  
  124. // 1 2 3 4 5 6 7 8 9
  125. // 1 8 3 6 5 4 7 2 9
  126.  
  127.  
  128.  
  129. // 1 2 3 4 5 6
  130. // 1 5 3 4 2 6
  131.  
  132. // 3
  133. // 1 5 3
Success #stdin #stdout 0s 2144KB
stdin
10
stdout
1-->2-->3-->4-->5-->6-->7-->8-->9-->10-->
1	
9	
3	
7	
5	
6	
4	
8	
2	
10