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++){
  60. p = p->next;
  61. }
  62. return p;
  63. }
  64.  
  65. int main(void){
  66. int i,n,x1;
  67. Node *k;
  68. scanf("%d",&n);
  69. initL(n);
  70. printL();
  71. scanf("%d",&x1);
  72. k = getN(x1);
  73. printN(k);
  74. freeL();
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5280KB
stdin
4
1 3 5 7
2
stdout
1 3 5 7 
3