-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList.cpp
More file actions
182 lines (165 loc) · 4.03 KB
/
LinkList.cpp
File metadata and controls
182 lines (165 loc) · 4.03 KB
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
#include <iostream>
#include <memory>
class MylistIterator;
class Linklist;
class Node
{
public:
friend class Linklist;
friend class MylistIterator;
Node(int data, Node* next) : _data(data), _next(next) {}
private:
int _data;
std::shared_ptr<Node> _next;
};
class MylistIterator{
public:
MylistIterator(std::shared_ptr<Node> cur):current(cur){}
int &operator*(){return current->_data;}
MylistIterator& operator++(){current=current->_next;
return *this;}
MylistIterator operator++(int){
auto temp=*this;
current=current->_next;
return temp;
}
bool operator!=(const MylistIterator& other){return current!=other.current;
}
private:
std::shared_ptr<Node> current;
};
class Linklist
{
friend class MylistIterator;
public:
explicit Linklist() : head(nullptr), length(0) {}
Linklist(const Linklist&) = delete;
Linklist(Linklist&&) = delete;
Linklist& operator=(const Linklist&) = delete;
Linklist& operator=(Linklist&&) = delete;
~Linklist();
void Insert(int key);
std::shared_ptr<Node> GetTarget(int key);
std::shared_ptr<Node> ReturnHead();
void Print();
void Delete(int key);
int GetLength();
void Destory();
MylistIterator begin();
MylistIterator end();
private:
std::shared_ptr<Node> head;
int length;
};
Linklist::~Linklist() { /*Destory();*/ }
std::shared_ptr<Node> Linklist::ReturnHead() { return head; }
void Linklist::Insert(int key)
{
auto new_node = std::make_shared<Node>(key, nullptr);
if (head == nullptr)
{
head = new_node;
}
else
{
auto index = head;
while (index->_next != nullptr)
{
index = index->_next;
}
index->_next = new_node;
}
length++;
}
[[nodiscard("can not ingore the returned")]]
std::shared_ptr<Node> Linklist::GetTarget(int key)
{
auto current = head;
if (current)
{
return current;
}
while (current->_data != key)
{
current = current->_next;
}
return current;
}
void Linklist::Print()
{
auto cur = head;
while (cur != nullptr)
{
std::cout << cur->_data << "->";
cur = cur->_next;
}
std::cout << "nullptr" << '\n';
}
void Linklist::Delete(int key)
{
if (!GetLength())
{
std::cout << "Delete Error" << '\n';
return;
}
auto index = head;
if (index->_data == key)
{
if (index->_next == nullptr)
{
index.reset();
head = nullptr;
}
else
{
while ((index->_next->_data != key) && (index->_next != nullptr))
{
index = index->_next;
}
auto flag = index->_next;
index->_next = flag->_next;
flag->_next = nullptr;
flag.reset();
}
length--;
}
}
MylistIterator Linklist::begin(){
return MylistIterator(head);
}
MylistIterator Linklist:: end(){
return MylistIterator(nullptr);
}
[[deprecated("it has deprecated")]] void Linklist::Destory()
{
while (head)
{
auto temp = head;
head = head->_next;
temp.reset();
}
std::cout << "资源回收";
}
int Linklist::GetLength() { return length; }
int main()
{
Linklist list;
list.Insert(11);
list.Delete(11);
list.Insert(12);
list.Insert(13);
list.Insert(14);
list.Print();
std::cout << "the length of list " << list.GetLength()<<std::endl;
for (auto it :list) {
std::cout<<it<<"->";
}
std::cout<<"nullptr";
return 0;
}
/*
这是基于c++98(后会稍有改动)的语言标准简单写的一个单链表,当然功能并不完善。
你也可以用c++11的标准来实现。最好不要手动释放内存,用RAII来管理会更好。
使用Clang在Vscode上面编译通过。GCC和MSVC应该也可以。
当然,如果支持Asan,可以试试clang++ -fsantize -g -std=c++xx target.cpp -o target
*/