How reverse a list into other list in C++

1 Answer

0 votes
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<char> lst, revlst;

	for (int i = 0; i < 5; i++)
		lst.push_back('a' + i);

	list<char>::iterator p;

	while (!lst.empty()) {
		p = lst.begin();
		cout << *p;
		revlst.push_front(*p);
		lst.pop_front();
	}
	cout << endl;

	p = revlst.begin();
	while (p != revlst.end()) {
		cout << *p;
		p++;
	}
	cout << endl;

	return 0;
}



/*
run:

abcde
edcba

*/

 



answered Apr 20, 2018 by avibootz
edited Apr 20, 2018 by avibootz
...