fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6. int data;
  7. struct node *next;
  8. } node;
  9.  
  10. node * mknode()
  11. {
  12. return malloc(sizeof(node));
  13. }
  14.  
  15. void create(node *h, int num)
  16. {
  17. int i = 0;
  18. h->data = i;
  19.  
  20. for(i = 1; i < num; i++)
  21. {
  22. h->next = mknode();
  23. h = h->next;
  24. h->data = i;
  25. }
  26. h->next = NULL;
  27. }
  28.  
  29. void display(node *h)
  30. {
  31. while(1)
  32. {
  33. printf("%d", h->data);
  34. h = h->next;
  35. if (h != NULL)
  36. printf("%s", "->");
  37. else
  38. break;
  39. }
  40. }
  41.  
  42. void append_end(node *h, int val)
  43. {
  44. while(h->next != NULL)
  45. h = h->next;
  46.  
  47. h->next = mknode();
  48. h->next->data = val;
  49. h->next->next = NULL;
  50. }
  51.  
  52. void free_list(node *h)
  53. {
  54. node * tail;
  55.  
  56. while(h != NULL)
  57. {
  58. tail = h->next;
  59. free(h);
  60. h = tail;
  61. }
  62. }
  63.  
  64. int main()
  65. {
  66. node * head = mknode();
  67. int num;
  68.  
  69. scanf("%d",&num);
  70. create(head, num);
  71.  
  72. append_end(head,5);
  73. append_end(head,6);
  74.  
  75. display(head);
  76. free_list(head);
  77. return 0;
  78. }
Success #stdin #stdout 0.01s 1856KB
stdin
4
stdout
0->1->2->3->5->6