Question #21390

Implement a function DisplayEven(...) that displays only the even elements inserted by the user. i.e.: element number 0, element number 2, etc.using double linked list

Expert's answer

#include <conio.h>
#include <list>
#include <string>
#include <iostream>

using namespace std;

void DisplayEven(list<string>& lst)
{
list<string>::iterator i = lst.begin();
for (int c = 0; c < lst.size(); c += 2)
{
cout << "Element " << c << ": " << *i << endl;
advance(i, 2);
}
}

void main()
{
list<string> lst;
string tmp;

cout << "Enter string values one by one or 'q' to stop:" << endl;
while (true)
{
getline(cin, tmp);
if (tmp == "q")
break;
lst.push_back(tmp);
}

cout << endl;
DisplayEven(lst);
_getch();
}
LATEST TUTORIALS
APPROVED BY CLIENTS