fork(2) 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 freeL(void){
  20. Node *p;
  21. while(head !=NULL){
  22. p=head->next;
  23. free(head);
  24. head=p;
  25. }
  26. }
  27.  
  28. void printL(void){
  29. Node *p=head;
  30. while(p !=NULL){
  31. printf("%d",p->val);
  32. p = p->next;
  33. }
  34. printf("\n");
  35. }
  36.  
  37. void makeL(int n,int a[]){
  38. if(n==0) return;
  39.  
  40. Node *p;
  41. head=createN(a[0]);
  42. p = head;
  43.  
  44. for (int i=1;i<n;i++){
  45. p->next=createN(a[i]);
  46. p=p->next;
  47. }
  48. }
  49.  
  50. int main(void){
  51. int i, n;
  52. int *a;
  53.  
  54. scanf("%d",&n);
  55. a=(int *)malloc(sizeof(int)*n);
  56.  
  57. for(i=0;i<n;i++){
  58. scanf("%d",&a[i]);
  59. }
  60.  
  61. makeL(n,a);
  62. printL();
  63. freeL();
  64. free(a);
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 5292KB
stdin
8
21 55 5 13 8 2 34 3
stdout
215551382343