fork(1) 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",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++)
  60. p=p->next;
  61. return p;
  62. }
  63.  
  64. int countL(){
  65. int ret=0;
  66. Node*p = head;
  67. while(p!=NULL){
  68. ret++;
  69. p=p->next;
  70. }
  71. return ret;
  72. }
  73.  
  74. int main(void){
  75. int i,n;
  76. scanf("%d",&n);
  77. initL(n);
  78. printL();
  79. printf("要素数:%d",countL());
  80. freeL();
  81. return 0;
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
1 3 5 7
stdout
1357
要素数:4