-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathLists.cpp
38 lines (26 loc) Β· 1.22 KB
/
Lists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//PLease read the text file Vectors.md in this directory before precedding further
#include<iostream>
#include <list>
using namespace std;
void showlist(list<int> mylist){
list <int>::iterator itr;//Making a iterator of my list as i cannot access index just like arrays
for(itr=mylist.begin();itr!=mylist.end();itr++){
cout<<" "<<*itr<<" "; //simple for loop to print the value of the element s in the list
}
cout<<endl;
}
int main(){
//Create a list
list <int> mylist;//convention basically to make a list of type int
//Now to push the data and perform the series of operations in the list
for(int i=1;i<=10;i++)
mylist.push_back(i*2); //vector::push_back() is a library function of "vector" header, it is used to insert/add an element at the end of the vector
cout<<"List is : ";showlist(mylist);//to show the list function is defined above
cout<<"Front : "<<mylist.front()<<"\t";//first
cout<<"Back : "<<mylist.back()<<endl;//last
cout<<"Pop front : "; mylist.pop_front(); showlist(mylist);
cout<<"Reverse : ";mylist.reverse();showlist(mylist);
cout<<"Sort : ";mylist.sort();showlist(mylist);
//There are other operations too but i have covered which are important and required
return 0;
}