Saturday, 24 January 2026

Circualr queue usign array implementation

  




#include <iostream>


using namespace std;


class Que{


    int arr[5];


    int front,rear;


    public:


        Que(){


            front=rear=-1;


        }


        void push(int x){

            

            if(front==-1){

                front=0;

                arr[++rear]=x;

                return;

            }

            int pos=((rear+1)%5);

            if(pos==front){

                cout<<"overflow";

                return;

            }

            rear=pos;

            arr[pos]=x;

            

            


        }


        int pop(){

            


            if(front==-1){


                cout<<"underflow";


                return -999;

            }

            int pos=((front+1)%5);

            if(front==rear){

                cout<<"underflow";

                return -999;

            }

            int x=arr[front];

            front=pos;


            return x;


        }


        void show(){

            int  temp=front;


            if(front!=-1){

                while(!(temp==rear)){

                    cout<<arr[temp]<<" ";

                    temp=(temp+1)%5;

                }

                cout<<arr[rear];



                cout<<endl;


            }


        }


    


};


int main()


{


    


    Que q;


    q.push(10);


    q.push(80);


    q.push(78);

    q.push(89);

    q.push(78);

    q.pop();

    q.push(23);

    q.pop();


    q.show();




    return 0;


}

No comments:

Post a Comment

SDE floyd-warshall algorithm

 // User function template for C++ class Solution {   public:     void floydWarshall(vector<vector<int>> &dist) {         //...