fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int key;
  7. struct node *next;
  8. };
  9.  
  10. void add(struct node **root,int key)
  11. {
  12. struct node *temp=(struct node*)malloc(sizeof(struct node));
  13. temp->key=key;
  14. temp->next=*root;
  15. *root=temp;
  16. }
  17.  
  18. void rem(struct node **root,int key)
  19. {
  20. struct node *temp,**curr;
  21. for(curr=root;*curr;curr=&(*curr)->next)
  22. {
  23. if((*curr)->key==key)
  24. {
  25. temp=*curr;
  26. *curr=temp->next;
  27. free(temp);
  28. break;
  29. }
  30. }
  31. }
  32.  
  33. void destroy(struct node *root)
  34. {
  35. struct node *temp;
  36. while(root)
  37. {
  38. temp=root;
  39. root=root->next;
  40. free(temp);
  41. }
  42. }
  43.  
  44. void show(struct node *list)
  45. {
  46. printf("{");
  47. for(;list;list=list->next) printf(" %d",list->key);
  48. printf(" }\n");
  49. }
  50.  
  51. int main()
  52. {
  53. struct node *list=NULL;
  54. add(&list,12); show(list);
  55. add(&list,14); show(list);
  56. add(&list,16); show(list);
  57.  
  58. rem(&list,16); show(list);
  59. rem(&list,12); show(list);
  60. rem(&list,14); show(list);
  61.  
  62. destroy(list);
  63. return 0;
  64. }
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
{ 12 }
{ 14 12 }
{ 16 14 12 }
{ 14 12 }
{ 14 }
{ }