#include<stdio.h>
#include<stdlib.h>// 定义单链表节点结构体
struct Node {int data;struct Node* next;
};struct Node* initList() {struct Node* list = (struct Node*)malloc(sizeof(struct Node));list->data = 0;list->next = NULL;return list;
}void headinsert(struct Node* list) {struct Node* tail = list;int num;printf("请输入整数序列(以0结束):\n");while (1) {scanf("%d", &num);if (num == 0) {break;}struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = num;newNode->next = NULL;tail->next = newNode;tail = newNode;}
}void printList(struct Node* head) {printf("链表的各个元素值为:\n");struct Node* current = head->next; // 跳过头节点while (current) {printf("%d ", current->data);current = current->next;}printf("\n");
}int findPosition(struct Node* head, int x) {int position = 0;head = head->next;while (head != NULL) {position++;if (head->data == x) {return position;}head = head->next;}return 0;
}void insertElement(struct Node* list, int i, int x) {struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = x;newNode->next = NULL;if (i <= 0) {newNode->next = list->next;list->next = newNode;}else {struct Node* current = list;int j = 0;while (current && j < i - 1) {current = current->next;j++;}if (current) {newNode->next = current->next;current->next = newNode;}else {printf("Error: Index out of range\n");free(newNode); // 释放新节点内存}}
}int countnumber(struct Node* head, int x) {int count = 0;head = head->next;while (head != NULL) {if (head->data == x) {count++;}head = head->next;}return count;
}int main() {struct Node* list = initList();headinsert(list);printList(list);int x = 0;printf("请输入要查找的元素的值:");scanf("%d", &x);int position = findPosition(list, x);if (position == 0) {printf("值为%d的元素不在链表中", x);printf("\n");}else{printf("值为%d的元素所在位置为%d", x, position);printf("\n");}int i = 0;int a = 0;printf("请输入要插入元素的值:");scanf("%d", &a);printf("请输入要插入元素的位置:");scanf("%d", &i);insertElement(list, i, a);printList(list);int b = 0;int number = 0;printf("请输入要查找元素个数的值:\n");scanf("%d", &b);number = countnumber(list, b);printf("值为%d的节点的个数为%d", b, number);return 0;
}
1. (算法设计题, 35分)设计算法实现将两个递增的带头结点有序链表合并为一个递增的有序链表,要求结果链表仍然使用原来两个链表的存储空间,表中不允许有重复的数据。
void mergeLists(struct ListNode* head1, struct ListNode* head2) {
struct ListNode* p1 = head1->next;
struct ListNode* p2 = head2->next;
struct ListNode* prev = head1;
while (p1 && p2) {
if (p1->val < p2->val) {
prev->next = p1;
p1 = p1->next;
} else if (p1->val > p2->val) {
prev->next = p2;
p2 = p2->next;
} else {
prev->next = p1; // 保留一个相同的节点
p1 = p1->next;
p2 = p2->next;
}
prev = prev->next;
}
if (p1)
prev->next = p1;
if (p2)
prev->next = p2;
}
初始化指针 p1 和 p2 分别指向两个链表的第一个实际节点。
初始化指针 prev 指向 head1,用于追踪结果链表的最后一个节点。
循环比较两个链表的节点值,将较小的节点接入结果链表,并更新对应链表的指针。
如果两个节点值相同,只保留一个节点。
最后,将剩余未遍历完的链表直接接到结果链表的末尾。算法实现将两个递增的带头结点有序链表合并为一个递增的有序链表,结果链表仍然使用原来两个链表的存储空间,没有重复的数据。
有一个带头结点的单链表L,编写在值为x的结点之后插入m个结点的算法。
void insertAfterX(struct ListNode* head, int x, int m) {
struct ListNode* current = head->next;
while (current != NULL && current->val != x) {
current = current->next;
}
int number=0;
if (current != NULL) {
for (int i = 0; i < m; i++) {
printf("请输入要插入节点的值:\n");
scanf("%d",&number);
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = number;
newNode->next = current->next;
current->next = newNode;
}
} else {
printf("没有找到值为%d的结点",x);
}
}
遍历链表,寻找值为 x 的结点。
如果找到了值为 x 的结点,则在其后插入 m 个新的结点。
新插入的结点值可以简单地从 1 开始递增。
如果未找到值为 x 的结点,则输出提示信息。