Advertisement
ruhan008

Queue implementation program

Nov 9th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. // 4) WAP to implement Queue
  2. #include <iostream>
  3. using namespace std;
  4. const int QueueSpace=10;
  5. int rear=-1;
  6. int front=0;
  7. int Queue[QueueSpace];
  8.  
  9. void push(int data){
  10.     if(rear==(QueueSpace-1)){
  11.         cout<<"Queue over flowed!!!"<<endl;
  12.         return ;
  13.     }
  14.     Queue[++rear] = data;
  15. }
  16.  
  17. void pop(){
  18.     if(front==rear+1){
  19.         cout<<"Queue under flowed!!!"<<endl;
  20.         return ;
  21.     }
  22.     front++;
  23. }
  24.  
  25. void peek(){
  26.     if(front==rear+1){
  27.         cout<<"Queue is empty!!!"<<endl;
  28.         return ;
  29.     }
  30.     cout<<"Current front element of Queue is: "<<Queue[front]<<endl;
  31. }
  32.  
  33. void size(){
  34.     cout<<"Current Queue size: "<<rear-front+1<<endl;
  35. }
  36.  
  37. void printQueue(){
  38.     cout<<"Current Queue elements: ";
  39.     for(int i=front;i<=rear;i++){
  40.         cout<<Queue[i]<<" ";
  41.     }
  42.     cout<<endl;
  43. }
  44. int main(){
  45.    
  46.     push(10);
  47.     push(20);
  48.     push(30);
  49.     push(40);
  50.     peek();
  51.     size();
  52.     printQueue();
  53.     pop();
  54.     peek();
  55.     size();
  56.     printQueue();
  57.    
  58.     return 0;
  59. }
  60. /*
  61.     OUTPUT:
  62.  
  63.     Current front element of Queue is: 10
  64.     Current Queue size: 4
  65.     Current Queue elements: 10 20 30 40
  66.     Current front element of Queue is: 20
  67.     Current Queue size: 3
  68.     Current Queue elements: 20 30 40
  69. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement