Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,126 people online right now. Registration is fast and FREE... Join Now!




stack

 
Reply to this topicStart new topic

stack, understanding stack

sknox1
29 Oct, 2006 - 11:35 AM
Post #1

New D.I.C Head
Group Icon

Joined: 30 Sep, 2006
Posts: 29


Dream Kudos: 150
My Contributions
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
User is offlineProfile CardPM
+Quote Post

horace
RE: Stack
29 Oct, 2006 - 12:57 PM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
the output would be
22 8 3
with y=8 and x=3

program with explaination
CODE

#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 Oct, 2006 - 12:58 PM
User is offlineProfile CardPM
+Quote Post

sknox1
RE: Stack
29 Oct, 2006 - 03:48 PM
Post #3

New D.I.C Head
Group Icon

Joined: 30 Sep, 2006
Posts: 29


Dream Kudos: 150
My Contributions
QUOTE(horace @ 29 Oct, 2006 - 01:57 PM) *

the output would be
22 8 3
with y=8 and x=3

program with explaination
CODE

#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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month