数据结构-实验二

实验二

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
#include <iostream>

using namespace std;

// 单链表节点结构
struct Node {
int data;
Node* next;
};

// 创建一个新节点
Node* createNode(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
return newNode;
}

// 插入节点到单链表尾部
void insertNodeAtEnd(Node*& head, int value) {
Node* newNode = createNode(value);

if (head == nullptr) {
head = newNode;
}
else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}

// 查找节点在单链表中的位置
int findNodePosition(Node* head, int value) {
int position = 1;
Node* current = head;
while (current != nullptr) {
if (current->data == value) {
return position;
}
current = current->next;
position++;
}
return -1; // 未找到
}

// 删除节点在单链表中的位置
bool deleteNodeAtPosition(Node*& head, int position) {
if (position < 1) {
return false; // 无效的位置
}

if (position == 1) {
Node* temp = head;
head = head->next;
delete temp;
return true;
}

Node* current = head;
int currentPosition = 1;
while (currentPosition < position - 1 && current != nullptr) {
current = current->next;
currentPosition++;
}

if (current == nullptr || current->next == nullptr) {
return false; // 位置超出链表长度
}

Node* temp = current->next;
current->next = current->next->next;
delete temp;
return true;
}

// 合并两个有序单链表
Node* mergeSortedLists(Node* list1, Node* list2) {
Node* mergedList = nullptr;
Node* current = mergedList;

while (list1 != nullptr && list2 != nullptr) {
if (list1->data < list2->data) {
insertNodeAtEnd(current, list1->data);
list1 = list1->next;
}
else {
insertNodeAtEnd(current, list2->data);
list2 = list2->next;
}

if (mergedList == nullptr) {
mergedList = current;
}
}

// 处理剩余的元素
while (list1 != nullptr) {
insertNodeAtEnd(current, list1->data);
list1 = list1->next;
}

while (list2 != nullptr) {
insertNodeAtEnd(current, list2->data);
list2 = list2->next;
}

return mergedList;
}

// 打印单链表
void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;

// 建立单链表
for (int i = 1; i <= 5; i++) {
insertNodeAtEnd(list1, i * 2);
insertNodeAtEnd(list2, i * 2 - 1);
}

cout << "List 1: ";
printList(list1);

cout << "List 2: ";
printList(list2);

// 合并有序单链表
Node* mergedList = mergeSortedLists(list1, list2);

cout << "Merged List: ";
printList(mergedList);

// 查找节点位置
int searchValue = 6;
int position = findNodePosition(mergedList, searchValue);
if (position != -1) {
cout << "Found " << searchValue << " at position " << position << endl;
}
else {
cout << searchValue << " not found in the merged list." << endl;
}

// 删除节点
int deletePosition = 3;
if (deleteNodeAtPosition(mergedList, deletePosition)) {
cout << "Deleted node at position " << deletePosition << endl;
cout << "Updated List: ";
printList(mergedList);
}
else {
cout << "Unable to delete node at position " << deletePosition << endl;
}

// 释放内存
while (mergedList != nullptr) {
Node* temp = mergedList;
mergedList = mergedList->next;
delete temp;
}

return 0;
}


数据结构-实验二
http://example.com/2024/02/05/实验二/
作者
yanhuigang
发布于
2024年2月5日
许可协议