-
Notifications
You must be signed in to change notification settings - Fork 0
/
test15_18.cpp
156 lines (144 loc) · 2.99 KB
/
test15_18.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
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct Node {
int val;
Node* next;
Node(int n, Node* next_ = nullptr) :
val(n), next(next_) {}
};
void addNode(Node* head, int n)
{
Node* p = head;
Node* newNode = new Node{n, nullptr};
while (p->next)
{
p = p->next;
}
newNode->val = n;
newNode->next = nullptr;
p->next = newNode;
}
Node* crateList(int mode, int num = 10, int x = 1)
{
Node* list = new Node{-1, nullptr};
if (mode == 1) {
for (int i = 1; i < num; i++)
{
addNode(list, rand() % 100 * x);
}
}
else
{
for (int i = 1; i < num+1; i++)
{
addNode(list, i * x);
}
}
return list;
}
void printNode(Node* head)
{
Node* p = head->next;
while (p)
{
std::cout << p->val << ' ';
p = p->next;
}
std::cout << endl;
}
void moveElements(Node* la, Node* lb, int index, int len)
{
Node* pa = la,
* pb = lb;
for (int i = 0; i < index-2; ++i)
{
pa = pa->next;
pb = pb->next;
}
Node* start = pa;
for (size_t i = 0; i < len; i++)
{
pa = pa->next;
}
Node* end = pa;
//放在la上
pa = start;
start = pa->next;
pa->next = end->next;
//放在lb上
end->next = pb->next;
pb->next = start;
}
void insertAfterX(Node * head, int x, int m) {
Node* p = head->next;
while (p != nullptr && p->val != x) {
p = p->next;
}
if (p == nullptr) {
printf("Error: Node with value %d not found.\n", x);
return;
}
for (int i = 0; i < m; i++) {
Node* newNode = new Node(0, p->next);
p->next = newNode;
p = newNode;
}
}
void insertBeforeI(Node * head, int i, int x) {
Node* prev = NULL;
Node* p = head;
for (int j = 1; j < i; j++) {
prev = p;
p = p->next;
}
Node* newNode = new Node(x);
if (prev == NULL) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = prev->next;
prev->next = newNode;
}
}
void deleteI(Node * head, int i) {
Node* prev = NULL;
Node* p = head;
for (int j = 1; j < i; j++) {
prev = p;
p = p->next;
}
if (prev == NULL) {
head = p->next;
}
else {
prev->next = p->next;
}
free(p);
}
int main()
{
Node* l1{},
* l2{};
l1 = crateList(2, 10, 1);
//l2 = crateList(2, 10, 100);
//cout << "两条链表初始值:" << endl;
//printNode(l1);
//printNode(l2);
//moveElements(l1->next, l2->next, 3, 4);//假装没有头节点
//cout << "moveElement后:" << endl;
//printNode(l1);
//printNode(l2);
//cout << "---------------" << endl;
//cout << "insertAfterX后:" << endl;
//insertAfterX(l1, 8, 5);
//printNode(l1);
printNode(l1);
insertBeforeI(l1->next, 6, 11111);
deleteI(l1->next, 6); //假设没有头节点
printNode(l1);
return 0;
}