Thread: stack problem in c++

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    Question stack problem in c++

    Code:
    #include<iostream.h>
    #include<conio.h>
    #define size 10
    class stack
    {
     int stck;
     int tos;
     public:
     stack();
     void push(int x);
     int pop();
    };
    stack::stack()
    {
    cout<<"constructing stack:\n";
    tos=0;
    }
    void stack::push(int x)
    {
    if(tos>=size)
      {
       cout<<"stack is full:\n";
       return;
       }
    tos++;
    stck[tos]=x;
    }
    int stack::pop()
    {
    int x;
    if(tos==0)
       {
        cout<<"stack is empty:\n";
        return 0;
        }
       x=stck[tos];
       tos--;
       return x;
    }
    int main()
    {
    stack s1,s2;
    int i,tos;
    clrscr();
    cout<<"after push operation:\n";
    for(i=0;i<=tos;i++)
      {
       s1.push(i);
       s2.push(i);
       }
    cout<<"after pop operation:\n";
    for(i=tos;i>=0;i--)
           {
                 s1.pop();
                s2.pop();
           }
    getch();
    return 0;
    }
    this is my final code now...there is no compiling error
    but it is still not running properly.when i run this it displays in the whole screen only 1 msg
    that "stack is full"and keeps on displaying that,
    why is that happening? now /?there is no compiling error
    but it is not running well...and also
    it is not taking any value from the user to push or pop...
    what do i do?now/?what i want to do in this code is actually
    i want the user to push some values of his choice into a stack
    and show the push operation and after that perform the pop operation
    for that stack and display it.
    : : but it is not doing that,so what r the necessary changes that
    i need to make in my code to make it happen..please help me out here,i really don't have a clue what to do?any kind of help would be much appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #define size 10
    
    class stack
    {
        int stck[size];
        int tos;
    public:
        stack();
        void push(int x);
        int pop();
    };
    Do you mean for your stck data member to be an array (see above highlight in red)? Following code makes that assumption.

    Code:
    stack::stack()
    {
        cout<<"constructing stack:\n";
        tos=0;
    }
    
    void stack::push(int x)
    {
        if(tos>=size)
        {
            cout<<"stack is full:\n";
            return;
        }
        tos++;
        stck[tos]=x;
    }
    Your push function starts at the wrong place. Your constructor sets tos to 0. When calling push for the first time, tos gets incremented to 1 and then we set stck[1]. Well... what happened to stck[0]? We have lost one of the available slots to store information. This also means that when tos is 9, we increment it to 10 and then set stck[10] which is an invalid array index.

    Usually when I see stacks get implemented, the top gets set to -1 and the push looks something like:

    Code:
    void stack::stack()
    {
        tos = -1;
    }
    
    void stack::push(int x)
    {
        if(tos+1==size)
        {
            cout<<"stack is full:\n";
            return;
        }
        stck[++tos]=x;
    }
    I would then implement the pop function like so:

    Code:
    int stack::pop()
    {
        if(tos==-1)
        {
            cout<<"stack is empty:\n";
            return 0;
        }
        return stck[tos--];
    }
    I would also implement an IsEmpty function so you would not have to rely upon getting an "is empty" message and an automatic return value of 0 in the for loop in main during the pop operations.

    Code:
    bool stack::IsEmpty()
    {
        return tos == -1;
    }
    
    ...
    
    while( !s1.IsEmpty() && !s2.IsEmpty() )  // Changed from a for loop
    {
        s1.pop();
        s2.pop();
    }
    Code:
    int main()
    {
        stack s1,s2;
        int i,tos;
        clrscr();
        cout<<"after push operation:\n";
        for(i=0;i<=tos;i++)
        {
            s1.push(i);
            s2.push(i);
        }
    
        ...
    tos is never initialized in main before its use. It's also a little confusing since you have a private member variable with the same name in your class. Also, the <= in the first for loop should be a < (assuming tos is meant to be initialized to size) unless it is your intention to test the "stack is full" message.

    Note: It would be extremely easy to turn this into a templated class where both the type and the default stack size could be given as template parameters. Could be done in a minute.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    so could u plz tell me what will the final code look like.?
    coz in the main function i m having a problem,,,,how will i be showing my 3 functions of push ,pop and isempty....and how would i be calling them.?
    i would really appreciate if u would plz write the final code,then i would be able to understand better.....

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Try This

    Quote Originally Posted by galmca
    so could u plz tell me what will the final code look like.?
    coz in the main function i m having a problem,,,,how will i be showing my 3 functions of push ,pop and isempty....and how would i be calling them.?
    i would really appreciate if u would plz write the final code,then i would be able to understand better.....
    Code:
    int num;
    //push
    for(i=o;i<tos;i++)
    {
     cout<<"enter the no.";
     cin>>num;
     s1.push(num);
    s2.push(num);
    }
    //pop
    for(i=tos;i>=0;i--)
           {
               
                if(!s1.pop()){
                cout<<"empty s1";break;}
               
                if(!s2.pop())
               {   cout<<"empty s2";break;}
           }

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    galmca, not to be rude or anything but the members here have told you countless of times to format your code and make it more readable. I remeber once Salem locked one of your threads because of this reason, he kept telling you to indent your code but you kept ignoring him like you know more than him. I told you that its a bad habit that you need to change. From little changes, you will see changes in the big picture.
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Problem?
    By audinue in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2009, 11:28 AM
  2. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Having problem deleting node in middle of stack
    By sballew in forum C Programming
    Replies: 3
    Last Post: 10-29-2001, 11:00 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM