-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleLinkedList.h
186 lines (167 loc) · 3.44 KB
/
SingleLinkedList.h
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
179
180
181
182
183
184
185
186
//
// Created by dima on 09.04.2022.
//
#ifndef MYAPP_SINGLELINKEDLIST_H
#define MYAPP_SINGLELINKEDLIST_H
#include "MyCollection.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
class SingleLinkedList : public MyCollection<T>
{
private:
struct Node
{
T value;
Node* next;
};
Node* head = nullptr;
Node* tail = nullptr;
public:
///////////////////////////////////////////////
virtual void Add(T const& t);
virtual void Remove(T const& t);
virtual void Print() const;
virtual bool Search(T const& t) const;
virtual void Sort();
///////////////////////////////////////////////
public:
bool IsEmpty();
private:
void Clear();
void FillFromVector(vector<T> const& vecT);
~SingleLinkedList();
vector<T> GetVector() const;
};
template<typename T>
void SingleLinkedList<T>::Add(const T& t)
{
Node* cur = new Node{ t, nullptr };
if (IsEmpty())
{
head = tail = cur;
}
else
{
tail->next = cur;
tail = cur;
}
++this->count;
}
template<typename T>
bool SingleLinkedList<T>::IsEmpty()
{
return this->count == 0;;
}
template<typename T>
void SingleLinkedList<T>::Print() const
{
Node* cur = head;
for (int i = 0; i < this->count; i++)
{
cout << cur->value << " " ;
if (i && i % 10 == 0) cout << endl;
cur = cur->next;
}
cout << endl;
}
template<typename T>
bool SingleLinkedList<T>::Search(const T &t) const
{
Node* cur = head;
for (int i = 0; i < this->count; i++)
{
int val = cur->value;
if (val == t)
{
return true;
}
cur = cur->next;
}
return false;
}
template<typename T>
void SingleLinkedList<T>::Remove(T const& t)
{
if (IsEmpty() || !Search(t)) return;
Node* l = nullptr;
Node* cur = head;
Node* r = head->next;
while (!IsEmpty() && cur != nullptr)
{
if (cur->value == t)
{
--this->count;
delete cur;
cur = nullptr;
if (l == nullptr) // first element
{
head = r;
}
else if (r == nullptr) // last element
{
}
else
{
l->next = r;
}
}
//cout << cur->value << endl;
l = cur;
cur = r;
if (cur == nullptr) continue;
r = r->next;
}
//cout << "\n$$$\n";
}
template<typename T>
void SingleLinkedList<T>::Sort()
{
vector<T> vecT = GetVector();
sort(vecT.begin(), vecT.end());
Clear();
FillFromVector(vecT);
}
template<typename T>
void SingleLinkedList<T>::Clear()
{
Node* cur = head;
this->count = 0;
for (int i = 0; i < this->count; i++)
{
Node* tmp = cur;
cur = cur->next;
delete tmp;
}
head = tail = nullptr;
}
template<typename T>
SingleLinkedList<T>::~SingleLinkedList()
{
Clear();
}
template<typename T>
vector<T> SingleLinkedList<T>::GetVector() const
{
vector<T> res;
Node* cur = head;
int result = -1;
for (int i = 0; i < this->count; i++)
{
int val = cur->value;
res.push_back(val);
cur = cur->next;
}
return res;
}
template<typename T>
void SingleLinkedList<T>::FillFromVector(const vector<T> &vecT)
{
for (auto const& e : vecT)
{
this->Add(e);
}
}
#endif //MYAPP_SINGLELINKEDLIST_H