用C语言完成文档要求
时间: 2024-11-27 09:23:09 浏览: 10
Object-C语言详解(文档)
根据文档《实验1-线性表1.docx》的要求,以下是使用C语言实现的代码示例。这些代码涵盖了实验中的主要任务,包括合并单链表和反转链表。
### 实验1:合并单链表
#### 单链表的定义
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
// 创建新节点
Node* createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
exit(0);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 插入节点到尾部
void insertAtEnd(Node **head, int data) {
Node *newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
```
#### 排序单链表
```c
// 交换两个节点的数据
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 冒泡排序
void sortList(Node *head) {
if (head == NULL || head->next == NULL) {
return;
}
Node *i, *j;
for (i = head; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
swap(&i->data, &j->data);
}
}
}
}
```
#### 求两个有序单链表的交集
```c
Node* intersect(Node *L1, Node *L2) {
Node *C = NULL;
Node *p1 = L1, *p2 = L2;
while (p1 != NULL && p2 != NULL) {
if (p1->data == p2->data) {
insertAtEnd(&C, p1->data);
p1 = p1->next;
p2 = p2->next;
} else if (p1->data < p2->data) {
p1 = p1->next;
} else {
p2 = p2->next;
}
}
return C;
}
```
#### 求两个有序单链表的差集
```c
Node* subtract(Node *L1, Node *L2) {
Node *C = NULL;
Node *p1 = L1, *p2 = L2;
while (p1 != NULL) {
while (p2 != NULL && p1->data > p2->data) {
p2 = p2->next;
}
if (p2 == NULL || p1->data < p2->data) {
insertAtEnd(&C, p1->data);
}
p1 = p1->next;
}
return C;
}
```
### 实验2:反转链表
#### 反转链表的部分
```c
Node* reverseBetween(Node *head, int left, int right) {
if (left == right) {
return head;
}
Node dummy(0);
dummy.next = head;
Node *prev = &dummy;
// Move prev to the node before the left position
for (int i = 0; i < left - 1; ++i) {
prev = prev->next;
}
Node *start = prev->next;
Node *then = start->next;
// Reverse the sublist from left to right
for (int i = 0; i < right - left; ++i) {
start->next = then->next;
then->next = prev->next;
prev->next = then;
then = start->next;
}
return dummy.next;
}
```
### 主函数
```c
int main() {
// 示例数据
Node *L1 = NULL;
Node *L2 = NULL;
insertAtEnd(&L1, 1);
insertAtEnd(&L1, 3);
insertAtEnd(&L1, 5);
insertAtEnd(&L2, 2);
insertAtEnd(&L2, 3);
insertAtEnd(&L2, 5);
// 排序
sortList(L1);
sortList(L2);
// 求交集
Node *intersectResult = intersect(L1, L2);
printf("Intersection: ");
Node *temp = intersectResult;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
// 求差集
Node *subtractResult = subtract(L1, L2);
printf("Difference: ");
temp = subtractResult;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
// 反转链表
Node *reversed = reverseBetween(L1, 2, 3);
printf("Reversed between 2 and 3: ");
temp = reversed;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
return 0;
}
```
以上代码实现了文档中要求的主要功能,包括创建单链表、排序、求交集、求差集以及部分反转链表。你可以根据需要进一步调整和优化代码。
阅读全文