-
Notifications
You must be signed in to change notification settings - Fork 0
/
warmup.cpp
66 lines (59 loc) · 1.44 KB
/
warmup.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
// 既然我们要用链表解题,那我们首先就构造一个链表吧 题目:给定数组 1,2,3,4 构造成如下链表 head–>4—->3—->2—->1
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
struct NODE {
int data;// 结点的数组域,值
NODE* next = nullptr;// 节点的引用,指向下一个节点
NODE(int inData)
{
data = inData;
}
};
class LINKED_LIST
{
private:
NODE* head = new NODE(0);
public:
LINKED_LIST() {}
void PrintLink()
{
NODE* tmp = head->next;
while (tmp != nullptr) {
cout << tmp->data << ' ';
tmp = tmp->next;
}
cout << endl;
}
void AddNode(int data)
{
NODE* tmp = head;
while (tmp->next != nullptr) {
tmp = tmp->next;
}
tmp->next = new NODE(data);
}
void HeadInsert(int data)
{
NODE* tmp = new NODE(data);
tmp->next = head->next;
head->next = tmp;
}
};
int main()
{
LINKED_LIST oneLink;
oneLink.AddNode(1);
oneLink.AddNode(2);
oneLink.AddNode(3);
oneLink.HeadInsert(4);
oneLink.PrintLink();
LINKED_LIST warmUp;
array<int, 5> arr = {1, 2, 3, 4, 5};
for_each(arr.begin(), arr.end(), [warmUp](int i) mutable {
warmUp.HeadInsert(i);
} );
warmUp.PrintLink();
return 0;
}