-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.cpp
178 lines (143 loc) · 2.62 KB
/
SinglyLinkedList.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
using namespace std;
template<typename T>
class vector
{
public:
vector();
~vector();
void pop_front();
void clear();
void push_back(T data);
int GetSize();
T& operator[] (const int index);
void push_front(T data);
void insert(T value, int index);
void removeAt(int index);
void pop_back();
private:
template<typename T>
class Node {
public:
Node* pNext_;
T data_;
Node(T data = T(), Node *pNext = nullptr)
: data_(data), pNext_(pNext){}
};
int size;
Node <T> *head;
};
template<typename T>
vector<T>::vector()
{
size = 0;
head = nullptr;
}
template<typename T>
vector<T>::~vector()
{
clear();
}
template<typename T>
void vector<T>::pop_front()
{
Node <T> *temp = head;
head = head->pNext_;
delete temp;
size--;
}
template<typename T>
void vector<T>::clear()
{
while (size) {
pop_front();
}
}
template<typename T>
void vector<T>::push_back(T data) {
if (head == nullptr) {
head = new Node <T>(data);
}
else {
Node<T>* current = head;
while (current->pNext_ != nullptr) {
current = current->pNext_;
}
current->pNext_ = new Node <T>(data);
}
size++;
}
int vector<int>::GetSize() {
return size;
}
template<typename T>
T& vector<T>::operator[](const int index)
{
int counter = 0;
Node<T>* current = head;
while (current !=nullptr) {
if (counter == index) {
return current->data_;
}
current = current->pNext_;
counter++;
}
}
template<typename T>
void vector<T>::push_front(T data)
{
head = new Node<T>(data, head);
size++;
}
template<typename T>
void vector<T>::insert(T value, int index)
{
if (index == 0) {
push_front(value);
}
else {
Node <T>* previous = head;
for (int i = 0; i < index-1; i++) {
previous = previous->pNext_;
}
previous->pNext_ = new Node<T>(value, previous->pNext_);
size++;
}
}
template<typename T>
void vector<T>::removeAt(int index)
{
if (index == 0) {
pop_front();
}
else {
Node <T>* previous = head;
for (int i = 0; i < index - 1; i++) {
previous = previous->pNext_;
}
Node<T> *toDelete = previous->pNext_;
previous->pNext_ = toDelete->pNext_;
delete toDelete;
size--;
}
}
template<typename T>
void vector<T>::pop_back()
{
removeAt(size - 1);
}
int main()
{
vector <int> number;
number.push_back(3);
number.push_back(7);
number.push_back(10);
number.push_back(1);
number.push_back(8);
number.insert(72, 3);
for (int i = 0; i < number.GetSize(); i++) {
cout << number[i] << endl;
}
return 0;
}