Stack<int> intStack;
Int x, y=3;
intStack.push(8);
intStack.push(9);
intStack.push(y);
x= intStack.top();
intstack.pop();
intstack.pop();
intStack.push(22);
while(!intstack.empty())
{y=intStack.top();
intStack.pop();
cout<<y<<” “;
}
cout<<x<<endl;
[output]
y=22
x=8
[quote]
Can anybody walk me through
or tell me if my output is correct
this is not a program
stackunderstanding stack
Page 1 of 1
2 Replies - 1305 Views - Last Post: 29 October 2006 - 04:48 PM
Replies To: stack
#2
Re: stack
Posted 29 October 2006 - 01:57 PM
the output would be
22 8 3
with y=8 and x=3
program with explaination
22 8 3
with y=8 and x=3
program with explaination
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> intStack;
int x, y=3;
intStack.push(8); // push 8
intStack.push(9); // push 9
intStack.push(y); // push y (3)
x= intStack.top(); // x is 3 (top of stack)
intStack.pop(); // pop 3
intStack.pop(); // pop 9
intStack.push(22); // push 22
// two items on stack 22 (top) and 8
while(!intStack.empty())
{y=intStack.top(); // y is top of stack
// - in first loop y is 22 second loop y is 8
intStack.pop(); // pop top of stack
cout<<y<<" "; // print y is 22 then 8
}
cout<<x<<endl; // print x is 3
cin.get();
cin.get();
return 0;
}
This post has been edited by horace: 29 October 2006 - 01:58 PM
#3
Re: stack
Posted 29 October 2006 - 04:48 PM
horace, on 29 Oct, 2006 - 01:57 PM, said:
the output would be
22 8 3
with y=8 and x=3
program with explaination
22 8 3
with y=8 and x=3
program with explaination
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> intStack;
int x, y=3;
intStack.push(8); // push 8
intStack.push(9); // push 9
intStack.push(y); // push y (3)
x= intStack.top(); // x is 3 (top of stack)
intStack.pop(); // pop 3
intStack.pop(); // pop 9
intStack.push(22); // push 22
// two items on stack 22 (top) and 8
while(!intStack.empty())
{y=intStack.top(); // y is top of stack
// - in first loop y is 22 second loop y is 8
intStack.pop(); // pop top of stack
cout<<y<<" "; // print y is 22 then 8
}
cout<<x<<endl; // print x is 3
cin.get();
cin.get();
return 0;
}
thank you horace for the walk through of these codes i now have a better understanding.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|