List

#include<iostream>
#include<list>
using namespace std;

int main() {

	/* Creating integer type list */
	list <int> lst;

	/* Declaration of list iterator */
	list<int>::iterator i;

	/* Insert data at beginning of the list */
	lst.insert(lst.begin(),4);
	lst.insert(lst.begin(),3);
	lst.insert(lst.begin(),2);
	lst.insert(lst.begin(),1);

	/* Insert data at end of the list */
	lst.insert(lst.end(),5);
	lst.insert(lst.end(),6);
	lst.insert(lst.end(),7);
	lst.insert(lst.end(),8);

	/* Print values from the list  */
	for(i = lst.begin(); i != lst.end(); i++)
		cout << *i << endl;
	cout << endl;

	/* Erease one element from the beginning */
	lst.erase(lst.begin());

	/* Checking, list is empty or not */
	if(!lst.empty()) {

		/* Print the size of the list */
		cout << "List size is: " << lst.size() << endl;
		for(i = lst.begin(); i != lst.end(); i++)
			cout << *i << endl;
		cout << endl;
	}

	return 0;
}



/* Output */
1
2
3
4
5
6
7
8

List size is: 7
2
3
4
5
6
7
8

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!