stack question

P

pentiumPunk

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!
 
K

Kristofer Pettijohn

pentiumPunk said:
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
 
L

llewelly

pentiumPunk said:
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.
 
K

Kevin Goodsell

pentiumPunk said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top