stack question

Discussion in 'C++' started by pentiumPunk, Sep 1, 2003.

  1. pentiumPunk

    pentiumPunk Guest

    string something;

    while(condition)
    {
    cin>> something;
    node<T> *x;
    x = new node<T> (something);
    aStack.push(x);
    }

    i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
    but the problem is...... x is being overridden each time. how am i going to
    manage the nodes in the stack ? thanks!
     
    pentiumPunk, Sep 1, 2003
    #1
    1. Advertising

  2. pentiumPunk <> wrote:
    > string something;
    >
    > while(condition)
    > {
    > cin>> something;
    > node<T> *x;
    > x = new node<T> (something);
    > aStack.push(x);
    > }
    >
    > i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
    > but the problem is...... x is being overridden each time. how am i going to
    > manage the nodes in the stack ? thanks!


    If what you want to keep adding to a stack is the variable 'something',
    you may want to read up on containers... Try...

    string something
    stack<string> aStack; // a stack of strings

    while (condition) {
    cin >> something;
    aStack.push(something);
    }

    Kristofer
     
    Kristofer Pettijohn, Sep 1, 2003
    #2
    1. Advertising

  3. pentiumPunk

    llewelly Guest

    "pentiumPunk" <> writes:

    > string something;
    >
    > while(condition)
    > {
    > cin>> something;
    > node<T> *x;
    > x = new node<T> (something);
    > aStack.push(x);
    > }
    >
    > i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
    > but the problem is...... x is being overridden each time. how am i going to
    > manage the nodes in the stack ? thanks!


    Maybe this:

    #include<string>
    #include<vector>
    #include<stack>
    #include<iostream>
    #include<ostream>

    using namespace std;

    int main()
    {
    string something;
    stack<string> astack;
    while(cin >> something)
    {
    astack.push(something);
    }

    cout << "\nDumping Stack." << endl;
    while(astack.size())
    {
    cout << astack.size() - 1 << " : " << astack.top() << endl;
    astack.pop();
    }
    }

    is helpful?

    If not, please explain the special features of your stack.
     
    llewelly, Sep 1, 2003
    #3
  4. pentiumPunk

    pentiumPunk Guest

    this was very helpful. thanks!
     
    pentiumPunk, Sep 1, 2003
    #4
  5. pentiumPunk wrote:

    > string something;
    >
    > while(condition)
    > {
    > cin>> something;
    > node<T> *x;
    > x = new node<T> (something);
    > aStack.push(x);
    > }
    >
    > i want to keep adding "something" to a stack, and top( ) and pop( ) it off,
    > but the problem is...... x is being overridden each time.


    "overridden"? How so? I know of nothing in C++ other than virtual
    functions that can be overridden, and I don't see that happening here.

    Perhaps you meant "overwritten"? If that's the case, I would agree that
    x is being overwritten each time, but I would disagree that this is a
    problem. The value of x ceases to be important once you've pushed it
    onto the stack.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,207
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,100
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,310
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    227
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    262
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page