Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. node *next;
  7.  
  8. };
  9. struct list
  10. {
  11. node *head;
  12. int len;
  13. void list_ini( )
  14. {
  15. head = new node;
  16. head->next = NULL;
  17.  
  18. }
  19. void list_end( )
  20. {
  21. node *tmp;
  22. node *cur=head;
  23. while(cur->next != NULL)
  24. {
  25. tmp= cur;
  26. cur=cur->next;
  27.  
  28. delete tmp;
  29. }
  30.  
  31. }
  32. void insert(node *x)
  33. {
  34. node *cur=head;
  35. while(cur->next!=NULL)
  36. cur=cur->next;
  37. cur->next=x;
  38. x->next = NULL;
  39.  
  40. }
  41. void show()
  42. {
  43. node *cur=head;
  44. while(cur->next!=NULL){
  45. printf("data %d\n",cur->next->data);
  46. cur=cur->next;}
  47.  
  48. }
  49.  
  50. void remove(node *y)
  51. {
  52. node *cur=head->next;
  53. if(cur)
  54. {
  55. head->next=head->next->next;
  56. printf("\n%d removed\n",cur->data);
  57.  
  58. delete cur;
  59. }
  60. else
  61. printf("\n Q empty \n");
  62.  
  63. }
  64. };
  65. int main()
  66. {
  67. int ch,x;
  68. node *y,*q;
  69.  
  70. list ob;
  71. ob.list_ini();
  72. while(1)
  73. {
  74. printf("\n1.Enqueue\n2.show\n3.Dequeue\n4.exit\n");
  75. scanf("%d",&ch);
  76. switch(ch)
  77. {
  78. case 1:
  79. {
  80. printf("enter no. ");
  81. scanf("%d",&x);
  82. y=new node;
  83. y->data=x;
  84. ob.insert(y);
  85. printf("data inserted....\n");
  86. break;
  87. }
  88. case 2:
  89. {
  90. ob.show();
  91. break;
  92. }
  93. case 3:
  94. {
  95. ob.remove(y);
  96. break;
  97. }
  98. case 4:
  99. {
  100. exit(0);
  101. }
  102. default:
  103. printf("wrong index\n");
  104.  
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement