Advertisement
Hydrase

Queue array

Aug 6th, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #define MAXSIZE 3
  5. class queue
  6. {
  7. int queue[MAXSIZE],front,rear;
  8. public:queue()
  9. {
  10. front=rear=-1;
  11. }
  12. void insert(void);
  13. void del(void);
  14. void display(void);
  15. };
  16. void queue::insert(void)
  17. {
  18. int item;
  19. if(rear==MAXSIZE-1)
  20. {
  21. cout<<"Queue Overflow"<<endl;
  22. return;
  23. }
  24. if(front==-1)
  25. front=rear=0;
  26. else
  27. rear=rear+1;
  28. cout<<"Enter the item to be inserted"<<endl;
  29. cin>>item;
  30. queue[rear]=item;
  31. display();
  32. }
  33. void queue::del(void)
  34. {
  35. int item;
  36. if(front==-1)
  37. {
  38. cout<<"Queue underflow"<<endl;
  39. return;
  40. }
  41. item=queue[front];
  42. cout<<"\nThe element deleted is"<<item<<endl;
  43. if(front==rear)
  44. front=rear-1;
  45. else
  46. front=front+1;
  47. display();
  48. }
  49. void queue::display(void)
  50. {
  51. if(front==-1)
  52. cout<<"Queue is empty"<<endl;
  53. else
  54. {
  55. for(int i=front;i<=rear;i++)
  56. cout<<queue[i]<<"\t";
  57. }
  58. }
  59. int main()
  60. {
  61. clrscr();
  62. int choice;
  63. char ch;
  64. queue obj;
  65. do
  66. {
  67. cout<<"\nMAIN MENU";
  68. cout<<"\n1.Insert\n2.Delete\n3.Display";
  69. cout<<"Enter your choice";
  70. cin>>choice;
  71. switch(choice)
  72. {
  73. case 1: obj.insert();
  74. break;
  75. case 2: obj.del();
  76. break;
  77. case 3: obj.display();
  78. break;
  79. default: cout<<"\nInvalid choice"<<endl;
  80. }
  81. cout<<"Do you wish to continue"<<endl;
  82. fflush(stdin);
  83. cin>>ch;
  84. }
  85. while(ch=='y'||'Y');
  86. getch();
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement