第2章习题
2.11设计算法以删除链表中所有值为x的元素结点。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// 在链表头部插入节点
void insertAtHead(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
// 删除链表中所有值为 x 的元素节点
void deleteNodesWithValue(Node** head, int x) {
Node* current = *head;
Node* previous = NULL;
while (current != NULL) {
if (current->data == x) {
if (previous == NULL) {
*head = current->next;
free(current);
current = *head;
} else {
previous->next = current->next;
free(current);
current = previous->next;
}
} else {
previous = current;
current = current->next;
}
}
}
// 遍历链表并打印节点的值
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// 在链表头部插入节点
insertAtHead(&head, 5);
insertAtHead(&head, 3);
insertAtHead(&head, 5);
insertAtHead(&head, 2);
insertAtHead(&head, 5);
insertAtHead(&head, 1);
// 删除链表中值为 5 的元素节点
int x = 5;
deleteNodesWithValue(&head, x);
// 遍历并打印链表
printf("链表中的元素: ");
printList(head);
return 0;
}
2.18假设递增有序的带头结点的单循环链表A、B分别表示两个集合,设计算法以求解A= A∪B,并分析算法的时间复杂度。
#include <iostream>
struct Node {
int data;
Node* next;
};
// 将链表B中的元素合并到链表A中
Node* mergeLists(Node* headA, Node* headB) {
if (headA == nullptr) {
return headB;
}
if (headB == nullptr) {
return headA;
}
Node* tailA = headA;
while (tailA->next != headA) {
tailA = tailA->next;
}
tailA->next = headB;
Node* tailB = headB;
while (tailB->next != headB) {
tailB = tailB->next;
}
tailB->next = headA;
return headA;
}
// 创建一个新节点
Node* createNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
// 在链表末尾插入节点
void insert(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
// 打印链表
void printList(Node* head) {
if (head == nullptr) {
return;
}
Node* temp = head;
do {
std::cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
std::cout << std::endl;
}
// 主函数
int main() {
Node* headA = nullptr;
Node* headB = nullptr;
// 在链表A中插入元素
insert(headA, 1);
insert(headA, 3);
insert(headA, 5);
// 在链表B中插入元素
insert(headB, 2);
insert(headB, 4);
insert(headB, 6);
std::cout << "链表A: ";
printList(headA);
std::cout << "链表B: ";
printList(headB);
Node* mergedList = mergeLists(headA, headB);
std::cout << "合并后的链表: ";
printList(mergedList);
return 0;
}
1. 初始化指针操作:O(1)
2. 遍历链表B找到尾节点:O(B)
3. 遍历链表A找到尾节点:O(A)
4. 指针操作:O(1)
因此,算法的总时间复杂度为 O(A + B)。
2.22设计算法将两个递增有序的带头结点的单链表A、B合并为一个递减有序的带头结点的单链表,并要求算法的时间复杂度为两个表长之和的数量级。
#include <iostream>
struct Node {
int data;
Node* next;
};
Node* mergeLists(Node* headA, Node* headB) {
if (headA == nullptr) {
return headB;
}
if (headB == nullptr) {
return headA;
}
Node dummy;
Node* headC = &dummy;
headC->next = nullptr;
while (headA != nullptr && headB != nullptr) {
if (headA->data <= headB->data) {
Node* temp = headA->next;
headA->next = headC->next;
headC->next = headA;
headA = temp;
} else {
Node* temp = headB->next;
headB->next = headC->next;
headC->next = headB;
headB = temp;
}
}
if (headA != nullptr) {
headC->next = headA;
}
if (headB != nullptr) {
headC->next = headB;
}
return dummy.next;
}
Node* createNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
void insert(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
int main() {
Node* headA = nullptr;
Node* headB = nullptr;
// 在链表A中插入元素
insert(headA, 1);
insert(headA, 3);
insert(headA, 5);
// 在链表B中插入元素
insert(headB, 2);
insert(headB, 4);
insert(headB, 6);
std::cout << "链表A: ";
printList(headA);
std::cout << "链表B: ";
printList(headB);
Node* mergedList = mergeLists(headA, headB);
std::cout << "合并后的链表: ";
printList(mergedList);
return 0;
}
(4)设计一个算法,删除递增有序链表中值大于mink且小于maxk的所有元素(mink和maxk是给定的两个参数,其值可以和表中的元素相同,也可以不同 )。
#include <iostream>
struct Node {
int data;
Node* next;
};
Node* removeElements(Node* head, int mink, int maxk) {
Node dummy;
dummy.next = head;
Node* prev = &dummy;
Node* curr = head;
while (curr != nullptr) {
if (curr->data < mink) {
prev = curr;
curr = curr->next;
} else if (curr->data >= mink && curr->data <= maxk) {
prev->next = curr->next;
delete curr;
curr = prev->next;
} else {
curr = curr->next;
}
}
return dummy.next;
}
Node* createNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
void insert(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(Node* head) {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
int main() {
Node* head = nullptr;
insert(head, 1);
insert(head, 2);
insert(head, 3);
insert(head, 4);
insert(head, 5);
int mink = 2;
int maxk = 4;
std::cout << "原始链表: ";
printList(head);
Node* modifiedList = removeElements(head, mink, maxk);
std::cout << "删除元素后的链表: ";
printList(modifiedList);
return 0;
}
记录一下今天是2024.3.15 /hfgydx/znkxyjs/2023217~~~/wyt