Creating a Stack and a Queue - C++ STL

C++ examples for STL:queue

Description

Creating a Stack and a Queue

Demo Code

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

void StackDemo(){
    stack<int, vector<int> > MyStack;

    MyStack.push(5);/*from w w w .  j  av  a 2  s  .  c  om*/
    MyStack.push(10);
    MyStack.push(15);
    MyStack.push(20);
    cout << MyStack.top() << endl;

    MyStack.pop();
    cout << MyStack.top() << endl;

    MyStack.pop();
    MyStack.push(40);
    cout << MyStack.top() << endl;

    MyStack.pop();
}

void QueueDemo()
{
    queue<int> MyQueue;

    MyQueue.push(5);
    MyQueue.push(10);
    MyQueue.push(15);
    cout << MyQueue.front() << endl;

    MyQueue.pop();
    cout << MyQueue.front() << endl;

    MyQueue.pop();
    MyQueue.push(40);
    cout << MyQueue.front() << endl;

    MyQueue.pop();
}

int main()
{
    StackDemo();
    QueueDemo();
    return 0;
}

Result


Related Tutorials