c++ stack and queue code example
Example: c++ stack and queue
#include
#include
#include
using namespace std;
void printStack(stack custstack)
{
for(int i = 0; i < 3; i++)
{
int y = custstack.top();
cout << y << endl;
custstack.pop();
}
}
void printQueue(queue custqueue)
{
for(int i = 0; i < 3; i++)
{
int y = custqueue.front();
cout << y << endl;
custqueue.pop();
}
}
int main ()
{
cout << "Stack:" << endl;
// this stack stacks three elements one by one and displays each element before its removal
stack MY_STACK; // define stack and initialize to 3 elements
MY_STACK.push(69); // last element popped / displayed
MY_STACK.push(68); // second element popped / displayed
MY_STACK.push(67); // third popped/displayed
printStack(MY_STACK);
cout << endl << "Switching to queue" << endl;
queue MY_QUEUE;
MY_QUEUE.push(69); // first out
MY_QUEUE.push(68); // second out
MY_QUEUE.push(67); // third out
printQueue(MY_QUEUE);
return 0;
}