implementing queue using stack in c++ code example

Example: c++ stack and queue

#include <iostream>
#include <stack> 
#include <queue>
using namespace std;

void printStack(stack<int> custstack)
{
    for(int i = 0; i < 3; i++)
    {
        int y = custstack.top();
        cout << y << endl;
        custstack.pop();
    }
}

void printQueue(queue<int> 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<int> 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<int> 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;
}

Tags:

Cpp Example