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. printf("a%lda", sizeof(Node));
  17. newnode->val = x;
  18. newnode->next = NULL;
  19. return newnode;
  20. }
  21.  
  22. void initL(int n){
  23. int x,i;
  24. Node *p;
  25. scanf("%d",&x);
  26. head = createN(x);
  27. p = head;
  28. for(i=1;i<n;i++){
  29. scanf("%d",&x);
  30. p->next = createN(x);
  31. p = p->next;
  32. }
  33. }
  34.  
  35. void freeL(){
  36. Node *p;
  37. while(head!=NULL){
  38. p = head->next;
  39. free(head);
  40. head = p;
  41. }
  42. }
  43.  
  44. void printN(Node *a){
  45. if(a == NULL) printf("NULL\n");
  46. else printf("%d\n",a->val);
  47. }
  48.  
  49. void printL(){
  50. Node *p = head;
  51. while(p != NULL){
  52. printf("%d ",p->val);
  53. p = p->next;
  54. }
  55. printf("\n");
  56. }
  57.  
  58. Node* getN(int n){
  59. int i;
  60. Node *p;
  61. p = head;
  62. for(i=1;i<n;i++){
  63. p = p->next;
  64. }
  65. return p;
  66. }
  67.  
  68. int main(void){
  69. int n,x1;
  70. Node *k;
  71. scanf("%d",&n);
  72. initL(n);
  73. printL();
  74. scanf("%d",&x1);
  75. k = getN(x1);
  76. printN(k);
  77. freeL();
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 5320KB
stdin
4
1 3 5 7
2
stdout
a16aa16aa16aa16a1 3 5 7 
3