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. Node *p,*q,*s;
  144. for(int i=0;i<n;i++){
  145. p=(Node*)malloc(sizeof(Node));
  146. p->val=a[i];
  147. p->next=NULL;
  148. if(head==NULL||head->val>=p->val){
  149. p->next=head;
  150. head=p;
  151. }else{
  152. q=head;
  153. s=head->next;
  154. while(s!=NULL&&s->val<p->val){
  155. q=s;
  156. s=s->next;
  157. }
  158. q->next=p;
  159. p->next=s;
  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. }
Success #stdin #stdout 0s 5288KB
stdin
8
v
N
H
J
R
D
S
A
stdout
0 0 0 0 0 0 0 0