stack question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pentiumPunk

    stack question

    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!





  • Kristofer Pettijohn

    #2
    Re: stack question

    pentiumPunk <Broox@triad.rr .com> wrote:[color=blue]
    > 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![/color]

    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(som ething);
    }

    Kristofer

    Comment

    • llewelly

      #3
      Re: stack question

      "pentiumPun k" <Broox@triad.rr .com> writes:
      [color=blue]
      > 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![/color]

      Maybe this:

      #include<string >
      #include<vector >
      #include<stack>
      #include<iostre am>
      #include<ostrea m>

      using namespace std;

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

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

      is helpful?

      If not, please explain the special features of your stack.

      Comment

      • pentiumPunk

        #4
        Re: stack question

        this was very helpful. thanks!



        Comment

        • Kevin Goodsell

          #5
          Re: stack question

          pentiumPunk wrote:
          [color=blue]
          > 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.[/color]

          "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 "overwritte n"? 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.

          Comment

          Working...