------------------------------------------------------------------------------
MC logo
stlist_cpp_TITLE
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[STL Vector 101] [STL Sorting Vectors] [stlist_cpp] [STL Map 1] [STL Map 2] [Word Counter]
stlist.cpp
#include <iostream>
#include <list>
#include <string>

using namespace std;

/*
 * Simple STL linked list demo.
 */
main()
{
        list<string> li;

        // Fill the list from input, adding each word to alternate ends.
        string in;
        bool putfront = true;
        while(cin >> in) {
                if(putfront) 
                        li.push_front(in);
                else
                        li.push_back(in);
                putfront = !putfront;
        }

        cout << "----------------------------------------"
             << "----------------------------------------" << endl;

        // Use an iterator to print the list forward.
        list<string>::iterator i;
        for(i = li.begin(); i != li.end(); ++i) {
                cout << *i << " ";
        }
        cout << endl;

        // Now print the list backwards.
        i = li.end();
        while(i != li.begin())
                cout << *--i << " ";
        cout << endl;
}