用C语言编程实现一个对单链表进行基本操作的系统,主要包括表的创建,输入,查询,取值,插入,删除,输出等操作
时间: 2024-05-29 16:10:49 浏览: 14
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct node {
int data;
struct node *next;
} Node;
// 创建单链表
Node *createList() {
Node *head = NULL, *tail = NULL;
int value;
printf("请输入链表节点的值,以-1结束:\n");
scanf("%d", &value);
while (value != -1) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
scanf("%d", &value);
}
return head;
}
// 输出单链表
void printList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 查询节点位置
int findNode(Node *head, int value) {
Node *p = head;
int index = 0;
while (p != NULL) {
if (p->data == value) {
return index;
}
p = p->next;
index++;
}
return -1;
}
// 获取节点值
int getNodeValue(Node *head, int index) {
Node *p = head;
int i = 0;
while (p != NULL) {
if (i == index) {
return p->data;
}
p = p->next;
i++;
}
return -1;
}
// 在指定位置插入节点
Node *insertNode(Node *head, int index, int value) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
return newNode;
}
if (index == 0) {
newNode->next = head;
return newNode;
}
Node *p = head;
int i = 0;
while (p != NULL) {
if (i == index-1) {
newNode->next = p->next;
p->next = newNode;
break;
}
p = p->next;
i++;
}
return head;
}
// 删除指定位置的节点
Node *deleteNode(Node *head, int index) {
if (head == NULL) {
return NULL;
}
if (index == 0) {
Node *temp = head;
head = head->next;
free(temp);
return head;
}
Node *p = head;
int i = 0;
while (p->next != NULL) {
if (i == index-1) {
Node *temp = p->next;
p->next = p->next->next;
free(temp);
break;
}
p = p->next;
i++;
}
return head;
}
int main() {
Node *head = createList();
printf("链表节点为:");
printList(head);
int value, index;
printf("请输入要查询的值:");
scanf("%d", &value);
index = findNode(head, value);
if (index == -1) {
printf("未找到该节点!\n");
} else {
printf("该节点位置为:%d\n", index);
}
printf("请输入要获取节点的位置:");
scanf("%d", &index);
value = getNodeValue(head, index);
if (value == -1) {
printf("未找到该节点!\n");
} else {
printf("该节点的值为:%d\n", value);
}
printf("请输入要插入的节点位置和值:");
scanf("%d %d", &index, &value);
head = insertNode(head, index, value);
printf("插入后节点为:");
printList(head);
printf("请输入要删除的节点位置:");
scanf("%d", &index);
head = deleteNode(head, index);
printf("删除后节点为:");
printList(head);
return 0;
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)