How to use list with push_back() and push_front() in C++

1 Answer

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

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

int main() 
{
	list<int> lst1, lst2;

	for (int i = 0; i < 5; i++)
		lst1.push_back(i);

	for (int i = 0; i < 5; i++)
		lst2.push_front(i);

	list<int>::iterator p;

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

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

	return 0;
}


/*
run:

0 1 2 3 4
4 3 2 1 0

*/

 



answered Apr 22, 2018 by avibootz

Related questions

2 answers 260 views
1 answer 132 views
1 answer 121 views
1 answer 191 views
1 answer 202 views
...