-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMySingleLinkedListSTLImplementation.cpp
100 lines (100 loc) · 2.93 KB
/
MySingleLinkedListSTLImplementation.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<list>
#include<iterator>
#include<math.h>
using namespace std;
void traverse(list<int> myList){
list<int>::iterator it;
for(it=myList.begin();it!=myList.end();++it){
cout<<"\t"<<*it;
}
cout<<"\n";
}
void sizeCheck(list<int> myList){
cout<<myList.size()<<endl;
}
void frontElement(list<int> myList){
cout<<"Front Element: "<<myList.front()<<endl;
}
void lastElement(list<int> myList){
cout<<"Last Element: "<<myList.back()<<endl;
}
void emptyCheck(list<int> myList){
if(myList.empty()==0){
cout<<"Empty: False"<<endl;
}else{
cout<<"Empty: ture"<<endl;
}
}
void popFront(list<int> myList){
cout<<"LinkedList before pop"<<endl;
traverse(myList);
cout<<"Deleted element: "<<myList.front()<<endl;
myList.pop_front();
traverse(myList);
}
void popBack(list<int> myList){
cout<<"LinkedList before pop"<<endl;
traverse(myList);
cout<<"Deleted element: "<<myList.back()<<endl;
myList.pop_back();
traverse(myList);
}
void reverseLinkedList(list<int> myList){
cout<<"LinkedList before reversal:"<<endl;
traverse(myList);
cout<<"LinkedList after reversal:"<<endl;
myList.reverse();
traverse(myList);
}
void mergeLinkedLists(list<int> myList,list<int> secondList){
myList.merge(secondList);
cout<<"LinkedList after merging"<<endl;
traverse(myList);
}
int main(){
list<int> myList,secondList;
for(int i=1;i<11;i++){
myList.push_back(i);
}
for(int i=2;i<12;i++){
secondList.push_back(pow(i,2));
}
cout<<"*************************************************"<<endl;
cout<<"Traverse the linkedlists:"<<endl;
traverse(myList);
traverse(secondList);
cout<<"*************************************************"<<endl;
cout<<"The size of the LinkedList:"<<endl;
sizeCheck(myList);
sizeCheck(secondList);
cout<<"*************************************************"<<endl;
cout<<"The front elemnt in the LinkedList:"<<endl;
frontElement(myList);
frontElement(secondList);
cout<<"*************************************************"<<endl;
cout<<"The last elemnt in the LinkedList:"<<endl;
lastElement(myList);
lastElement(secondList);
cout<<"*************************************************"<<endl;
cout<<"Empty check of LinkedList:"<<endl;
emptyCheck(myList);
emptyCheck(secondList);
cout<<"*************************************************"<<endl;
cout<<"Pop elements in the LinkedList"<<endl;
cout<<"###########################################"<<endl;
cout<<"POP FRONT:"<<endl;
popFront(myList);
popFront(secondList);
cout<<"###########################################"<<endl;
cout<<"POP BACK:"<<endl;
popBack(myList);
popBack(secondList);
cout<<"*************************************************"<<endl;
cout<<"Reverse of LinkedList:"<<endl;
reverseLinkedList(myList);
reverseLinkedList(secondList);
cout<<"*************************************************"<<endl;
cout<<"Merge two linkedlists:"<<endl;
mergeLinkedLists(myList,secondList);
}