UIU: Learn C++ STL by Examples : Stack and Queue



// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

int main (int argc, char *argv [])
{
    stack <int> stk;

    stk.push(5);
    stk.push(10);
    stk.push(15);

    if (stk.top() == 15) {
        printf ("stk top 15");
    }

    printf ("%lu\n", stk.size());

    stk.push(200);

    while (!stk.empty()) {
        printf ("%d\n", stk.top());
        stk.pop();
    }

    /*
    Queue
     */

    queue <int> q;

    q.push(10);
    q.push(15);
    q.push(20);
    q.push(25);

    if (q.front() == 10) {
        printf ("q front is 10");
    }

    printf ("%lu\n", q.size());

    printf ("%d\n", q.back());

    while (!q.empty()) {
        printf("%d", q.front());
        q.pop();
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s