stack

understanding stack

Page 1 of 1

2 Replies - 1350 Views - Last Post: 29 October 2006 - 04:48 PM Rate Topic: -----

#1 sknox1  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 30-September 06

stack

Post icon  Posted 29 October 2006 - 12:35 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: stack

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

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
#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

Was This Post Helpful? 0
  • +
  • -

#3 sknox1  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 30-September 06

Re: stack

Post icon  Posted 29 October 2006 - 04:48 PM

View Posthorace, on 29 Oct, 2006 - 01:57 PM, said:

the output would be
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1