设计算法,实现线性结构上的单链表的产生以及元素的查找、插入与删除。具体实现要求: (1)从键盘输入20个整数,用前插法或尾插法输入结点值。 (2)从键盘输入1个整数,在单链表中查找该结点的位置。若找到,则显示“找到了”;否则,则显示“找不到”。 (3)从键盘输入2个整数,一个表示欲插入的位置i,另一个表示欲插入的数值x,将x插入在对应位置上,输出单链表所有结点值,观察输出结果。 (4)从键盘输入1个整数,表示欲删除结点的位置,输出单链表所有结点值,观察输出结果。 (5)将单链表中值重复的结点删除,使所得的结果表中个结点值均不相同,输出单链表所有结点值,观察输出结果。 (6)删除其中所有数据值为偶数的结点,输出单链表所有结点值,观察输出结果。 (7)将单链表分解成两个单链表A和B,使A链表中含有原链表中序号为奇数的元素,而B链表中含有原链表中序号为偶数的元素,且保持原来的相对顺序,分别输出单链表A和单链表B的所有结点值,观察输出结果。用C语言
时间: 2024-02-13 18:02:08 浏览: 106
单链表的建立、插入、删除、查找.rar_单链表的建立、插入、删除、查找_插入删除
5星 · 资源好评率100%
以下是单链表的实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 头插法插入节点
void insertAtHead(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// 尾插法插入节点
void insertAtTail(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
Node* cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
}
// 查找节点位置
int findNode(Node* head, int value) {
int pos = 0;
Node* cur = head;
while (cur != NULL) {
pos++;
if (cur->data == value) {
printf("找到了,位置为%d\n", pos);
return pos;
}
cur = cur->next;
}
printf("找不到\n");
return -1;
}
// 插入节点
void insertNode(Node** head, int pos, int data) {
if (pos == 1) {
insertAtHead(head, data);
return;
}
Node* cur = *head;
for (int i = 1; i < pos-1 && cur != NULL; i++) {
cur = cur->next;
}
if (cur == NULL) {
printf("插入位置超出链表长度\n");
return;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = cur->next;
cur->next = newNode;
}
// 删除节点
void deleteNode(Node** head, int pos) {
if (*head == NULL) {
printf("链表为空\n");
return;
}
Node* cur = *head;
if (pos == 1) {
*head = cur->next;
free(cur);
return;
}
for (int i = 1; i < pos-1 && cur != NULL; i++) {
cur = cur->next;
}
if (cur == NULL || cur->next == NULL) {
printf("删除位置超出链表长度\n");
return;
}
Node* toDelete = cur->next;
cur->next = toDelete->next;
free(toDelete);
}
// 删除重复节点
void deleteDuplicate(Node** head) {
if (*head == NULL) {
printf("链表为空\n");
return;
}
Node* cur = *head;
while (cur != NULL) {
Node* innerCur = cur;
while (innerCur->next != NULL) {
if (innerCur->next->data == cur->data) {
Node* toDelete = innerCur->next;
innerCur->next = toDelete->next;
free(toDelete);
} else {
innerCur = innerCur->next;
}
}
cur = cur->next;
}
}
// 删除偶数节点
void deleteEven(Node** head) {
if (*head == NULL) {
printf("链表为空\n");
return;
}
Node* cur = *head;
Node* prev = NULL;
while (cur != NULL) {
if (cur->data % 2 == 0) {
if (prev == NULL) {
*head = cur->next;
} else {
prev->next = cur->next;
}
Node* toDelete = cur;
cur = cur->next;
free(toDelete);
} else {
prev = cur;
cur = cur->next;
}
}
}
// 分解链表
void splitList(Node* head, Node** aHead, Node** bHead) {
if (head == NULL) {
return;
}
Node* curA = NULL;
Node* curB = NULL;
Node* cur = head;
int index = 1;
while (cur != NULL) {
if (index % 2 != 0) {
if (curA == NULL) {
*aHead = cur;
curA = cur;
} else {
curA->next = cur;
curA = curA->next;
}
} else {
if (curB == NULL) {
*bHead = cur;
curB = cur;
} else {
curB->next = cur;
curB = curB->next;
}
}
cur = cur->next;
index++;
}
if (curA != NULL) {
curA->next = NULL;
}
if (curB != NULL) {
curB->next = NULL;
}
}
// 输出链表
void printList(Node* head) {
Node* cur = head;
while (cur != NULL) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
Node* aHead = NULL;
Node* bHead = NULL;
// 输入20个整数,用尾插法输入结点值
printf("请输入20个整数(用空格隔开):\n");
for (int i = 0; i < 20; i++) {
int num;
scanf("%d", &num);
insertAtTail(&head, num);
}
// 在单链表中查找节点
printf("请输入要查找的整数:\n");
int value;
scanf("%d", &value);
findNode(head, value);
// 插入节点
printf("请输入要插入节点的位置和数据(用空格隔开):\n");
int pos, data;
scanf("%d %d", &pos, &data);
insertNode(&head, pos, data);
printf("插入节点后的链表:\n");
printList(head);
// 删除节点
printf("请输入要删除节点的位置:\n");
scanf("%d", &pos);
deleteNode(&head, pos);
printf("删除节点后的链表:\n");
printList(head);
// 删除重复节点
deleteDuplicate(&head);
printf("删除重复节点后的链表:\n");
printList(head);
// 删除偶数节点
deleteEven(&head);
printf("删除偶数节点后的链表:\n");
printList(head);
// 分解链表
splitList(head, &aHead, &bHead);
printf("链表A:\n");
printList(aHead);
printf("链表B:\n");
printList(bHead);
return 0;
}
```
阅读全文