Advertisement
fahimkamal63

Queue Program

Apr 8th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. //  Queue Program
  2. //  Date : 08.04.19
  3. #include<iostream>
  4. #include<cstdlib>
  5. #define max_size 20
  6. using namespace std;
  7.  
  8. class queue_class{
  9.     int queue_array[max_size];
  10.     int front_index, rear_index;
  11.     void insert_queue();
  12.     void delete_queue();
  13.     void display();
  14. public:
  15.     queue_class(){ front_index = rear_index = 0; }
  16.     void control_function();
  17. };
  18.  
  19. void queue_class::control_function(){
  20.     while(1){
  21.         cout << "*****Welcome to Queue program*****\n"
  22.              << "Press 1 to Insert.\n"
  23.              << "Press 2 to Delete.\n"
  24.              << "Press 3 to Display the Queue.\n"
  25.              << "Press 4 to Exit.\n"
  26.              << "Enter Your choice: ";
  27.         int choice; cin >> choice;
  28.         system("cls");
  29.  
  30.         if(choice == 1) insert_queue();
  31.         else if(choice == 2) delete_queue();
  32.         else if(choice == 3) display();
  33.         else if(choice == 4) break;
  34.         else cout << "Wrong Choice.\n\n";
  35.     }
  36.     return;
  37. }
  38.  
  39. void queue_class::insert_queue(){
  40.     if(rear_index == max_size){
  41.         cout << "Queue Overflow\n";
  42.         return;
  43.     }
  44.     else{
  45.         cout << "Enter Element to insert: ";
  46.         cin >> queue_array[rear_index++];
  47.     }
  48. }
  49.  
  50. void queue_class::delete_queue(){
  51.     if(front_index == max_size || front_index == rear_index){
  52.         cout << "Queue Underflow\n";
  53.         return;
  54.     }
  55.     else{
  56.         cout << "Element " << queue_array[front_index++]
  57.              << " is deleted\n";
  58.     }
  59. }
  60.  
  61. void queue_class::display(){
  62.     if(front_index == max_size || front_index == rear_index){
  63.         cout << "Queue Underflow nothing to display!!!\n";
  64.         return;
  65.     }
  66.     else{
  67.         cout << "The elements in the Queue are: \n";
  68.         for(int i = front_index; i < rear_index; i++){
  69.             cout << queue_array[i] << ' ';
  70.         }
  71.         cout << endl;
  72.     }
  73. }
  74.  
  75. int main(){
  76.     queue_class ob1;
  77.     ob1.control_function();
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement