Advertisement
Foyaj128

Queue- Insertion & display

Apr 15th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8. void insertion(struct node *f,struct node*r,int vv);
  9. void display(struct node *f);
  10. int main()
  11. {
  12. int v,c;
  13. struct node *front,*rear;
  14. front=(struct node*)malloc(1*sizeof(struct node));
  15. rear=(struct node*)malloc(1*sizeof(struct node));
  16. front->data=0;
  17. front->next=NULL;
  18. rear->data=0;
  19. rear->next=NULL;
  20. while(1)
  21. {
  22. printf("\n----- MENU -----\n");
  23. printf("\n----- Press 1 for insertion -----\n");
  24.  
  25. printf("\n----- Press 3 for display -----\n");
  26. printf("\n----- Press 0 for quit -----\n");
  27. printf("\n Enter Your Choice \n");
  28. scanf("%d",&c);
  29. switch(c)
  30. {
  31. case 0:exit(0);
  32. break;
  33.  
  34. case 1:
  35. printf("\n Choice=insertion \n");
  36. printf("Enter New value \n");
  37. scanf("%d",&v);
  38. insertion(front,rear,v);
  39. break;
  40.  
  41.  
  42.  
  43. case 3:printf("\n Choice=Display \n");
  44. if(front->next!=NULL)
  45. display(front);
  46. else
  47. printf("\n The queue is Empty \n");
  48. break;
  49.  
  50.  
  51.  
  52. default:printf("\n Wrong choice\n\n");
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. void insertion(struct node *f,struct node*r,int vv)
  59. {
  60. struct node *temp;
  61. temp=(struct node*)malloc(1*sizeof(struct node));
  62. temp->data=vv;
  63. temp->next=NULL;
  64. if(f->next==NULL)
  65. {
  66. f->next=temp;
  67. r->next=temp;
  68. }
  69. else
  70. {
  71. r->next->next=temp;
  72. r->next=temp;
  73. }
  74.  
  75. }
  76. void display(struct node *f)
  77. {
  78. while(f->next!=NULL)
  79. {
  80. printf("\t %d",f->next->data);
  81. f=f->next;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement