Home > Software engineering >  My stack is not displaying my array but queue display it
My stack is not displaying my array but queue display it

Time:11-19

My Queue is displaying right but my stack is displaying nothing. Im using array to transfer it to stack and to queue but when i transfer the array to stacks and display it, it show nothing. and is there a way to reverse my Queue and save it to my array again?

case 1 is where the input of stack and queue comes, void display in class stack is the problem I copied my whole stack on my stack project where I array transfer to stack but now when I transfer the array to stack and queue, stack gone missing.

class Stack { private:
    int MAX;
    int top;
    int *grd_s;

public:
    Stack (int size)
    {
        MAX=size;
        top=-1;
        grd_s=new int[MAX];
    }

    void push(int Q)
    {
    if ((top 1)==MAX)
        cout << "Stack Overflow..." << endl;
    top  ;
    grd_s[top]=Q;
    }

    void display(int ctr)
    {
    cout<<"Cloud contains --"<<endl;
    for (int ctr=top; ctr>=0; ctr--)
    {
        if (ctr==top)
            cout<<"\t\t"<<grd_s[ctr]<<"<--Top of Stack"<<endl;
        else
            cout<<"\t\t"<<grd_s[ctr]<<endl;
    }
    } };

class Queue { private:
    int front;
    int rear;
    int grd_q[5]; public:
    Queue()
    {
        front=-1;
        rear=-1;
        for (int z=0; z<5; z  )
        {
            grd_q[z]=0;
        }
    }
    bool isEmpty()
    {
        if (front==-1||rear==-1)
            return true;
        else
            return false;
    }
    bool isFull()
    {
        if (rear==4)
            return  true;
        else
            return false;
    }
    void insert(int val)
    {
        if(isFull())
        {
            cout<<"Queue is Full..."<<endl;
            return;
        }
        else if (isEmpty())
        {
            rear=0;
            front=0;
            grd_q[rear]=val;
        }
        else
        {
            rear  ;
            grd_q[rear]=val;
        }

    }
    void show()
    {
        cout<<"Local contains: "<<endl;
        for (int x=0; x<5; x  )
        {
            cout<<grd_q[x]<<" ";
        }
    }
    void rer()
    {
        cout<<"sad"<<endl;
    }

};

case 1:
    cout <<"Enter your 5 Grades" << endl;
    for (int i=0; i<5; i  )
        {
            cout << "   " << i 1 << " . ";
            cin >> grd;
            Q[i] = grd;
        } system("cls"); break;

CodePudding user response:

You need to construct your Stack outside the do-while loop.

As it stands right now, a new Stack is created at each iteration while the previous goes out of scope. In doing so, you are creating your Stack in the input phase, only for it to get erased at end of the iteration!

Just modify the beginning of your main() function:

int main()
{
    Queue q1;
    Stack st(5);    // Put this line here, outside the loop!
    int Q[5];
    int choice;
    int chc;
    int chc2;
    int grd;

    system("cls");
 
do
{
    //  Stack st(5); Not here !
    displayRules();
    displayRules1();
    cin>>choice;
   ...
  •  Tags:  
  • c
  • Related