fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5. int val;
  6. struct node *next;
  7. }Node;
  8.  
  9. Node *head = NULL;
  10.  
  11. Node* createN(int x){
  12. Node *newnode;
  13. newnode = (Node *)malloc(sizeof(Node));
  14. newnode->val = x;
  15. newnode->next = NULL;
  16. return newnode;
  17. }
  18.  
  19. void initL(int n){
  20. int x,i;
  21. Node *p;
  22. scanf("%d",&x);
  23. head = createN(x);
  24. p = head;
  25. for(i=1;i<n;i++){
  26. scanf("%d",&x);
  27. p->next = createN(x);
  28. p = p->next;
  29. }
  30. }
  31.  
  32. void freeL(){
  33. Node *p;
  34. while(head!=NULL){
  35. p = head->next;
  36. free(head);
  37. head = p;
  38. }
  39. }
  40.  
  41. void printN(Node *a){
  42. if(a == NULL) printf("NULL\n");
  43. else printf("%d\n",a->val);
  44. }
  45.  
  46. void printL(){
  47. Node *p = head;
  48. while(p != NULL){
  49. printf("%d ",p->val);
  50. p = p->next;
  51. }
  52. printf("\n");
  53. }
  54.  
  55. Node* getN(int n){
  56. int i;
  57. Node *p;
  58. p = head;
  59. for(i=1;i<n;i++) p = p->next;
  60. return p;
  61. }
  62.  
  63. int countL(){
  64. int ret = 0;
  65. Node *p = head;
  66. while(p!=NULL){
  67. p = p->next;
  68. ret++;
  69. }
  70. return ret;
  71. }
  72.  
  73. Node* searchX(int x){
  74. Node *p;
  75. for(p=head; p!=NULL; p=p->next){
  76. if(p->val == x) break;
  77. }
  78. return p;
  79. }
  80.  
  81. void insHead(int x){
  82. Node *p; //1
  83. p = createN(x); //1
  84. p->next = head; //2
  85. head = p; //3
  86. }
  87.  
  88. void insMiddle(int n, int x){
  89. int i;
  90. Node *p,*q;
  91. p = head; //1
  92. for(i=1;i<n;i++){ //2
  93. p = p->next; //2
  94. }
  95. q = createN(x); //3
  96. q->next = p->next; //4
  97. p->next = q; //5
  98. }
  99.  
  100. void insTail(int x){
  101. Node *p;
  102. p = head; //1
  103. if(p==NULL){
  104. head = createN(x);
  105. return;
  106. }
  107. while(p->next != NULL){ //2
  108. p = p->next; //2
  109. }
  110. p->next = createN(x); //3
  111. }
  112.  
  113. void delHead(){
  114. Node *p;
  115. p = head; //1
  116. head = head->next; //2
  117. free(p); //3
  118. }
  119.  
  120. void delMiddle(int n){
  121. int i;
  122. Node *p,*q;
  123. p = head; //1
  124. for(i=1;i<n-1;i++){ //2
  125. p = p->next; //2
  126. }
  127. q = p->next; //3
  128. p->next = q->next; //4
  129. free(q); //5
  130. }
  131.  
  132. void delTail(){
  133. Node *p;
  134. p = head; //1
  135. while(p->next->next != NULL){ //2
  136. p = p->next; //2
  137. }
  138. free(p->next); //3
  139. p->next = NULL; //4
  140. }
  141.  
  142. void makeL(int n, int a[]){
  143. int i,j;
  144. Node *p;
  145. insHead(a[0]);
  146. for(i=1;i<n;i++){
  147. p=head;
  148. for(j=0;p!=NULL;j++){
  149. if(a[i]<p->val) break;
  150. p=p->next;
  151. }
  152. if(j==0) insHead(a[i]);
  153. else if(j==countL()) insTail(a[i]);
  154. else insMiddle(j,a[i]);
  155. }
  156. }
  157.  
  158. int main(void){
  159. int i,n;
  160. int *a;
  161. scanf("%d",&n);
  162. a = (int*)malloc(sizeof(int)*n);
  163. for(i=0;i<n;i++){
  164. scanf("%d",&a[i]);
  165. }
  166. makeL(n,a);
  167. printL();
  168. freeL();
  169. return 0;
  170. }
  171.  
Success #stdin #stdout 0s 5288KB
stdin
8
21
55
5
13
8
2
34
3
stdout
2 3 5 8 13 21 34 55