fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int info;
  6. struct node *link;
  7. };
  8. struct node *addtobeg(struct node *start,int data)
  9. {
  10. struct node *tmp;
  11. tmp=(struct node *)malloc(sizeof(struct node));
  12. tmp->info=data;
  13. tmp->link=NULL;
  14. start=tmp;
  15. return start;
  16. }
  17. struct node *addtoend(struct node *start,int data)
  18. {
  19. struct node *tmp,*p;
  20. if(start==NULL)
  21. {
  22. printf("No 1st element:\n");
  23. return start;
  24. }
  25. p=start;
  26. while(p->link!=NULL)
  27. p=p->link;
  28. tmp=(struct node *)malloc(sizeof(struct node));
  29. tmp->info=data;
  30. tmp->link=p->link;
  31. p->link=tmp;
  32. return start;
  33. }
  34. void display(struct node *start)
  35. {
  36. struct node *p;
  37. if(start==NULL)
  38. {
  39. printf("Empty list:\n");
  40. return;
  41. }
  42. p=start;
  43. printf("\n");
  44. while(p!=NULL)
  45. {
  46. printf(" %d",p->info);
  47. p=p->link;
  48. }
  49. printf("\n");
  50. }
  51. struct node *create_list(struct node *start)
  52. {
  53. start=addtobeg(start,23);
  54. start=addtoend(start,67);
  55. start=addtoend(start,89);
  56. start=addtoend(start,7);
  57. start=addtoend(start,34);
  58. return start;
  59. }
  60. struct node *deletes(struct node *start)
  61. {
  62. struct node *current,*next;
  63. current=start;
  64. while(current!=NULL)
  65. {
  66. next=current->link;
  67. free(current);
  68. start=next;
  69. current=next;
  70. }
  71. start=NULL;
  72. return start;
  73. }
  74. int main()
  75. {
  76. struct node *start;
  77. start=NULL;
  78. start=create_list(start);
  79. display(start);
  80. printf("After deletion:\n");
  81. start=deletes(start);
  82. display(start);
  83. return 0;
  84. }
Success #stdin #stdout 0s 1964KB
stdin
Standard input is empty
stdout
 23 67 89 7 34
After deletion:
Empty list: