文章 单链表的C++实现
文章
取消

单链表的C++实现

单链表的C++实现

自己独立实现一遍,记忆深刻。

实现

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#pragma once
#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Node
{
public:
    const T _data;
    Node<T> *_next; // 下一个结点

    Node() : _data(0)
    {
        _next = nullptr;
    };
    Node(const T &v) : _data(v)
    {
        _next = nullptr;
    };
};

template <class T>
class LinkList
{
private:
    Node<T> *_head;  // 头结点
    Node<T> *_stern; // 尾结点
    Node<T> *_temp;  // 操作实现的中间使用变量
    mutable size_t _size;

public:
    LinkList();                                   // 构建一个单链表;
    LinkList(const T &v);                         // 头结点设置为特殊值(如果有需要)
    ~LinkList();                                  // 销毁一个单链表;
    void CreateLinkList(vector<T> &n);            // 使用一个向量创建一个单链表
    void CreateLinkList(T n[], size_t len);       // 使用一个数组创建一个单链表
    void CreateLinkList(size_t len);              // cin输入创建一个单链表
    size_t GetSize();                             // 获取线性表长度
    bool IsEmpty();                               // 判断单链表是否为空
    vector<int> Find(const T &data);              // 查找节点
    vector<int> ForDeleteFind(const T &data);     // 查找结点,返回该值的索引递减数组,该序列只能用于删除操作
    Node<T> *FindIndex(int index);                // 查找索引节点
    void InsertElemAtEnd(const T &data);          // 在尾部插入指定的元素
    void InsertElemAtIndex(const T &data, int n); // 在指定位置插入指定元素
    void InsertElemAtHead(const T &data);         // 在头部插入指定元素
    void DeleteElemAtEnd();                       // 在尾部删除元素
    void DeleteElemAtIndex(int n);                // 删除索引元素
    void DeleteAll();                             // 删除所有数据
    void DeleteElemAtPoint(const T &data);        // 删除指定的数据
    void DeleteElemAtHead();                      // 在头部删除节点
    void Show();                                  // 展示链表
};

// 创建一个空链表
template <class T>
LinkList<T>::LinkList() : _size(0)
{
    this->_head = this->_stern = this->_temp = new Node<T>();
};

// 创建一个头结点是特殊值的空链表
template <class T>
LinkList<T>::LinkList(const T &v) : _size(0)
{
    this->_head = this->_stern = new Node<T>(v);
    this->_temp = nullptr;
};

// 销毁一个单链表;
template <class T>
LinkList<T>::~LinkList()
{
    delete _head;
};

// 使用vector创建一个单链表
template <class T>
void LinkList<T>::CreateLinkList(vector<T> &n)
{
    size_t size = n.size();
    for (size_t i = 0; i < size; i++)
        this->InsertElemAtEnd(n[i]);
};

// 使用一个数组创建单链表
template <class T>
void LinkList<T>::CreateLinkList(T n[], size_t len)
{
    this->_temp = this->_head;
    for (size_t i = 0; i < len; i++)
        this->InsertElemAtEnd(n[i]);
}

// cin创建链表
template <class T>
void LinkList<T>::CreateLinkList(size_t len)
{
    T value;
    for (size_t i = 0; i < len; i++)
    {

        cout << "输入第" << i + 1 << "元素:";
        cin >> value;
        InsertElemAtEnd(value);
    }
}

// 尾部插入
template <class T>
void LinkList<T>::InsertElemAtEnd(const T &data)
{
    Node<T> *newnd = new Node<T>(data);
    this->_stern->_next = newnd;
    this->_stern = newnd;
    this->_size++;
};

// 头插
template <class T>
void LinkList<T>::InsertElemAtHead(const T &data)
{
    Node<T> *newnd = new Node<T>(data);
    this->_temp = this->_head->_next;
    this->_head->_next = newnd;
    newnd->_next = this->_temp;
    this->_size++;
}

// 查找结点,返回该值的索引数组
template <class T>
vector<int> LinkList<T>::Find(const T &data)
{
    vector<int> value = vector<int>();
    this->_temp = this->_head;
    int j = 0;
    while (this->_temp != nullptr)
    {
        if (data == this->_temp->_data)
        {
            value.push_back(j);
        }
        j++;
        this->_temp = this->_temp->_next;
    }
    return value;
}

// 查找结点,返回该值的索引递减数组,该序列只能用于删除操作
template <class T>
vector<int> LinkList<T>::ForDeleteFind(const T &data)
{
    vector<int> value = vector<int>();
    this->_temp = this->_head;
    int i = 0, j = 0;
    while (this->_temp != nullptr)
    {
        if (data == this->_temp->_data)
        {
            value.push_back(j - i);
            i++;
        }
        j++;
        this->_temp = this->_temp->_next;
    }
    return value;
}

// 使用索引查找结点
template <class T>
Node<T> *LinkList<T>::FindIndex(int index)
{
    this->_temp = this->_head;
    if (index > this->_size || index < 0)
        return nullptr;
    if (index == 0)
        return this->_head;
    for (int l = index; l > 0; l--)
        this->_temp = this->_temp->_next;
    return this->_temp;
}

// 在索引位置插入元素
template <class T>
void LinkList<T>::InsertElemAtIndex(const T &data, int n)
{
    if (n > this->_size + 1 || n <= 0)
        return;
    if (n == this->_size + 1)
        this->InsertElemAtEnd(data);
    this->_temp = FindIndex(n - 1);
    Node<T> *newnd = new Node<T>(data);
    Node<T> *tnd = this->_temp->_next;
    this->_temp->_next = newnd;
    newnd->_next = tnd;
    this->_size++;
}

// 删除该索引位置元素
template <class T>
void LinkList<T>::DeleteElemAtIndex(int n)
{
    if (n > this->_size || n <= 0)
        return;
    this->_temp = FindIndex(n - 1);
    Node<T> *tnd = this->_temp->_next;
    this->_temp->_next = this->_temp->_next->_next;
    if (n == this->_size)
        this->_stern = this->_temp;
    delete tnd;
    tnd = nullptr;
    this->_temp = nullptr;
    this->_size--;
}

// 在尾部删除元素
template <class T>
void LinkList<T>::DeleteElemAtEnd()
{
    if (this->_size == 0)
        return;
    Node<T> *tnd = this->FindIndex(this->_size - 1);
    tnd->_next = nullptr;
    this->_stern = tnd;
    this->_size--;
}

// 删除所有元素
template <class T>
void LinkList<T>::DeleteAll()
{
    this->_temp = this->_head->_next;
    this->_head->_next = nullptr;
    delete this->_temp;
    this->_temp = nullptr;
    this->_stern = this->_head;
    this->_size = 0;
}

//删除所有的值等于的结点
template <class T>
void LinkList<T>::DeleteElemAtPoint(const T &data)
{
    vector<int> indexs = this->ForDeleteFind(data);
    for (auto i : indexs)
        this->DeleteElemAtIndex(i);
}

//删除头结点
template <class T>
void LinkList<T>::DeleteElemAtHead()
{
    if (this->_head->_next == nullptr)
        return;
    this->_temp = this->_head->_next;
    this->_head->_next = this->_head->_next->_next;
    delete this->_temp;
    this->_temp = nullptr;
    this->_size--;
}

// 表是否为空
template <class T>
bool LinkList<T>::IsEmpty()
{
    return this->_size == 0;
}

// 表长
template <class T>
size_t LinkList<T>::GetSize()
{
    return this->_size;
}

template <class T>
void LinkList<T>::Show()
{
    this->_temp = this->_head->_next;
    while (this->_temp != nullptr)
    {
        if (this->_temp->_next == nullptr)
            cout << this->_temp->_data;
        else
            cout << this->_temp->_data << "->";

        this->_temp = this->_temp->_next;
    }
    cout << endl;
    this->_temp = this->_head;
};

测试

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
#include <iostream>
#include "head/List.h"
#include <vector>

using namespace std;

template<typename T>
void log(T &a)
{
    cout << a << endl;
}

int main()
{
    vector<int> s = vector<int>();

    for (size_t i = 0; i < 5; i++)
    {
        s.push_back(i * i);
    }
    for(int a=0;a<10;a++){
        if(a % 2 == 0)
            s.push_back(25);
        else
            s.push_back(12);
    }

    LinkList<int> lists = LinkList<int>();

    lists.CreateLinkList(s);
    lists.Show();
    log("删除25:");
    lists.DeleteElemAtPoint(25);
    lists.Show();
    log("删除尾:");
    lists.DeleteElemAtEnd();
    lists.Show();
    log("删除头:");
    lists.DeleteElemAtHead();
    lists.Show();
    log("删除第四个:");
    lists.DeleteElemAtIndex(4);
    lists.Show();
    log("尾部插入:");
    lists.InsertElemAtEnd(114);
    lists.Show();
    log("头部插入:");
    lists.InsertElemAtHead(154);
    lists.Show();
    log("第一个位置插入:");
    lists.InsertElemAtIndex(366,1);
    lists.Show();
    cout << "长度为:" <<  lists.GetSize() << endl;
    cout << "是否为空:" << lists.IsEmpty() << endl;
    lists.DeleteAll();
    cout << "长度为:" <<  lists.GetSize() << endl;
    cout << "是否为空:" << lists.IsEmpty() << endl;
    return 0;
}

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0->1->4->9->16->25->12->25->12->25->12->25->12->25->12
删除25:
0->1->4->9->16->12->12->12->12->12
删除尾:
0->1->4->9->16->12->12->12->12
删除头:
1->4->9->16->12->12->12->12
删除第四个:
1->4->9->12->12->12->12
尾部插入:
1->4->9->12->12->12->12->114
头部插入:
154->1->4->9->12->12->12->12->114
第一个位置插入:
366->154->1->4->9->12->12->12->12->114
长度为:10
是否为空:0
长度为:0
是否为空:1
本文由作者按照 CC BY 4.0 进行授权

红黑树

黑苹果Venture 13.4 Intel无线网卡(AX200,AX201,AX210,AX203)蓝牙问题排查