#include <iostream>
#include <list>
#include <string>
using namespace std;
/*
* Simple STL linked list demo.
*/
int main()
{
list<string> li;
// Fill the list from input, adding each word to alternate ends.
string in;
bool putfront = true;
cout << "Enter words: " << endl;
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.
for(auto i = li.begin(); i != li.end(); ++i) {
cout << *i << " ";
}
cout << endl;
// Now print the list backwards.
auto i = li.end();
while(i != li.begin())
cout << *--i << " ";
cout << endl;
}
Textbook: pp. 376—383
[more info]