Using the Stack Class
Capabilities Description |
All Code in the Main Function |
Function Prototypes, Separate Create and Print Functions |
back | Returns a reference to the last and most recently added element at the back of the stack. |
empty | Tests if the stack is empty |
front | Returns a reference to the first element at the front of the queue |
pop | Removes an element from the front of the stack |
push | Adds an element to the back of the stack |
size | Returns the size of the stack |
#include
"stdafx.h"
#include
<iostream>
#include
<stack>
#include
<string>
using
namespace
std;
int main()
{
stack<string>
allwords; // stack of all words
string word;
// input buffer for words.
// read words/tokens from input stream
for (int i
= 1; i<4; i++)
{
cout <<"Please ente a word"<<endl;
cin>>word;
allwords.push(word);
}
cout <<
"Number of words = " << allwords.size()
<< endl;
// write out all the numbers in reverse order.
while (!allwords.empty())
{
cout << allwords.top() << endl;
allwords.pop();
// remove top element
}
return 0;
}
#include
"stdafx.h"
#include
<iostream>
#include
<stack>
#include
<string>
using
namespace
std;
void
CreateArray(int A[]);
void
PrintArray(int A[]);
void
CreateStack(stack<string> &bucket);
void
PrintStuff(stack<string> &bucket);
int
main()
{
stack<string> allwords;
int A[3];
CreateArray(A);
CreateStack(allwords);
PrintStuff(allwords);
PrintArray(A);
return 0;
}
void
CreateStack(stack<string> &bucket)
{
string word;
cout <<"Please ente a word"<<endl;
cin>>word;
bucket.push(word);
}
void
PrintStuff(stack<string> &bucket)
{
cout<<"The Stack"<<endl;
cout << "Number of words = " <<
bucket.size() << endl;
while (!bucket.empty())
{
cout << bucket.top() << endl;
bucket.pop();
}
}
void
CreateArray(int A[])
{
for
(int i = 0; i<3; i++)
{
A[i] = 1;
}
}
void
PrintArray(int A[])
{
cout<<"The Array"<<endl;
for
(int i = 0; i<3; i++)
{
cout<<A[i]<<endl;
}
}
Output