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 *newNode=createN(a[i]);
  147.  
  148. if(head==NULL || head->val >=a[i]){
  149. newNode->next=head;
  150. head=newNode;
  151. }else{
  152. Node *p;
  153. p=head;
  154.  
  155. while(p->next!=NULL && p->next->val<a[i]){
  156. p=p->next;
  157. }
  158. newNode->next=p->next;
  159. p->next=newNode;
  160. }
  161. }
  162. }
  163.  
  164. int main(void){
  165. int i,n;
  166. int *a;
  167. scanf("%d",&n);
  168. a = (int*)malloc(sizeof(int)*n);
  169. for(i=0;i<n;i++){
  170. scanf("%d",&a[i]);
  171. }
  172. makeL(n,a);
  173. printL();
  174. freeL();
  175. return 0;
  176. }
  177.  
Success #stdin #stdout 0.01s 5284KB
stdin
8
21 55 5 13 8 2 34 3
stdout
2 3 5 8 13 21 34 55