Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed queue and stack went wrong

+2 votes
asked Oct 2 by Venkata Koushik Pemma (160 points)
closed Oct 4 by Admin
I have written a code consisting of both and stack and queue (since the question I was asked by my teacher required it). Here's the code :

#include <iostream>

#include <cstring>

#define SIZE 100

using namespace std;

class Queue

{

    private:

        int front,rear;

        char Qstore[SIZE];

    public :

    Queue()

    {

    front = -1;

    rear = -1;

    // for(int i=0;i<SIZE;i++)

    // Qstore[i]="O";

    }

    void enqueueCharacter(char ch)

    {

    if(rear==SIZE-1 || front>rear)

    cout<<"Queue overflow\n";

    else

    {Qstore[++rear]=ch;}

    if(front==-1)

    front++;

    }

    char dequeueCharacter()

    {

    char ele;

    if(front==-1 || front>rear)

    cout<<"Queue underflow\n";

    else

    ele=Qstore[front++];

    return ele;

    }

    void displayQ()

    {

    if(front==-1)

    cout<<"Queue underflow\n";

    else

    {

    for(int i=front;i<=rear;i++)

    cout<<Qstore[i]<<" ";

    }

    }

};

class Stack

{

private:

int top;

char Sstore[SIZE];

public:

Stack()

{

top=-1;

// for(int i=0;i<SIZE;i++)

// Sstore[i]="O";

}

void pushCharacter(char ch)

{

if(top==SIZE-1)

cout<<"Stack overflow\n";

else

Sstore[++top]=ch;

}

char popCharacter()

{

char ele;

if(top==-1)

cout<<"Stack underflow\n";

else

{

ele=Sstore[top];

top--;

}

return ele;

}

void displayS()

{

if(top==-1)

cout<<"Stack underflow\n";

else

{

for(int i=top;i>=0;i++)

cout<<Sstore[i]<<" ";

}

}

};

int main()

{

Queue q;

Stack s;

int n_stk,n_q;

char ele_stk,ele_q;

cout<<"enter the size of stack..."; cin>>n_stk;

for(int i=0;i<n_stk;i++)

{

cout<<"enter the element : "; cin>>ele_stk;

s.pushCharacter(ele_stk);

}

s.displayS();

cout<<"\n\n";

cout<<"enter the sizeof queue..."; cin>>n_q;

for(int j=0;j<n_q;j++)

{

cout<<"enter the element : "; cin>>ele_q;

q.enqueueCharacter(ele_q);

}

q.displayQ();

return 0;

}

I don't know why but after taking input from stack, the output is some weird character followed by a beep sound (which scared the crap out of me). It would be great if anyone tries to fix my issue :))

(I actually need to do even more functions to that thing and also the deadline is approaching :')
closed with the note: answered

1 Answer

+1 vote
answered Oct 3 by Peter Minarik (34,450 points)

The problem in the code is with your loop in void Stack::displayS() as it is iterating in the wrong direction. You start i from top and go as long as it is not less than 0, but in the iteration part, you do not decrease i's value (correct) but increase it instead (incorrect).

Your code correctly would be like this:

void displayS()
{
    if (top == -1)
        cout << "Stack underflow\n";
    else
    {
        for (int i = top; i >= 0; i--)
            cout << Sstore[i] << " ";
    }
}

After this, your classes seem to work correctly.

I do not know what you need to do here, but here are some improvement opportunities:

  1. you have hardwired the size of your containers to be no more than SIZE. You don't need to do this. With the help of dynamic allocation, you can set the underlying array's size to any arbitrary number.
  2. with the help of generics, you can store any arbitrary type in your containers.
commented Oct 3 by Venkata Koushik Pemma (160 points)
Thank you very much for solving the error and suggesting to improve my code. I actually don't know about generics and stuff, but I will try to take your suggestion into consideration :))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...