forked from neilchauhan2/Git_first
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListCPP.cpp
142 lines (118 loc) · 2.82 KB
/
LinkedListCPP.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
// You should use templates when you do not know what type of data will be
// stored in List
template<typename T>
// In Node we store value of a current node and a pointer for next node
struct Node {
T data;
Node* nextNode;
};
template<typename T>
// This class is responsible for creating, inserting and deleting nodes or
// values in our List
class List{
private:
Node<T>* listHead;
Node<T>* listTail;
public:
List() :listHead(nullptr), listTail(nullptr) {}
void createNode(T value);
void insertValue(T value);
void insertValueAtPosition(int pos, T value);
void deleteFirstElement();
void deleteLastElement();
void deleteElementAtPosition(int pos);
void showElementsOfList();
};
template<typename T>
void List<T>::createNode(T value)
{
Node<T>* tmp = new Node<T>;
tmp->data = value;
tmp->nextNode = nullptr;
if (listHead == nullptr) {
listHead = tmp;
listTail = tmp;
tmp = nullptr;
} else {
listTail->nextNode = tmp;
listTail = tmp;
}
}
template<typename T>
void List<T>::insertValue(T value)
{
Node<T>* tmp = new Node<T>;
tmp->data = value;
tmp->nextNode = listHead;
listHead = tmp;
}
template<typename T>
void List<T>::insertValueAtPosition(int pos, T value)
{
Node<T>* pre = new Node<T>;
Node<T>* current = new Node<T>;
Node<T>* tmp = new Node<T>;
current = listHead;
for (int i = 1; i < pos; i++) {
pre = current;
current = current->nextNode;
}
tmp->data = value;
pre->nextNode = tmp;
tmp->nextNode = current;
}
template<typename T>
void List<T>::deleteFirstElement()
{
Node<T>* tmp = new Node<T>;
tmp = listHead;
listHead = listHead->nextNode;
delete tmp;
}
template<typename T>
void List<T>::deleteLastElement()
{
Node<T>* current = new Node<T>;
Node<T>* previous = new Node<T>;
current = listHead;
while (current->nextNode != nullptr) {
previous = current;
current = current->nextNode;
}
listTail = previous;
previous->nextNode = nullptr;
delete current;
}
template<typename T>
void List<T>::deleteElementAtPosition(int pos)
{
Node<T>* current = new Node<T>;
Node<T>* previous = new Node<T>;
current = listHead;
for (int i = 1; i < pos; i++) {
previous = current;
current = current->nextNode;
}
previous->nextNode = current->nextNode;
}
template<typename T>
void List<T>::showElementsOfList()
{
Node<T>* tmp = new Node<T>;
tmp = listHead;
while (tmp != nullptr) {
std::cout<<tmp->data<<"\n";
tmp = tmp->nextNode;
}
}
int main()
{
List<int> objList;
objList.createNode(21);
objList.createNode(37);
objList.createNode(14);
objList.createNode(88);
objList.showElementsOfList();
return 0;
}