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