用C语言定义main函致,调用给出的建立,输出,插入,刷除等函效。验证单链表
时间: 2024-03-11 12:46:50 浏览: 23
以下是一个用 C 语言定义 main 函数,调用单链表的建立、输出、插入和删除等函数的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构体
typedef struct node {
int data;
struct node *next;
} Node;
// 建立单链表
Node *createLinkedList(int n) {
Node *head = NULL, *tail = NULL;
int i, val;
for (i = 1; i <= n; i++) {
printf("请输入第 %d 个节点的值:", i);
scanf("%d", &val);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 输出单链表
void printLinkedList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 插入节点
void insertNode(Node **head, int pos, int val) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
if (pos == 1) {
newNode->next = *head;
*head = newNode;
return;
}
Node *p = *head;
int i;
for (i = 1; i < pos - 1 && p != NULL; i++) {
p = p->next;
}
if (p == NULL) {
printf("插入位置超出链表长度!\n");
return;
}
newNode->next = p->next;
p->next = newNode;
}
// 删除节点
void deleteNode(Node **head, int pos) {
if (*head == NULL) {
printf("链表为空,无法删除节点!\n");
return;
}
if (pos == 1) {
Node *p = *head;
*head = (*head)->next;
free(p);
return;
}
Node *p = *head;
int i;
for (i = 1; i < pos - 1 && p != NULL; i++) {
p = p->next;
}
if (p == NULL || p->next == NULL) {
printf("删除位置超出链表长度!\n");
return;
}
Node *q = p->next;
p->next = q->next;
free(q);
}
int main() {
int n, pos, val;
printf("请输入链表长度:");
scanf("%d", &n);
// 建立单链表
Node *head = createLinkedList(n);
// 输出单链表
printf("建立的单链表为:");
printLinkedList(head);
// 插入节点
printf("请输入要插入的位置和值:");
scanf("%d %d", &pos, &val);
insertNode(&head, pos, val);
printf("插入后的单链表为:");
printLinkedList(head);
// 删除节点
printf("请输入要删除的位置:");
scanf("%d", &pos);
deleteNode(&head, pos);
printf("删除后的单链表为:");
printLinkedList(head);
return 0;
}
```
该程序首先让用户输入链表长度,然后调用 createLinkedList 函数建立单链表,调用 printLinkedList 函数输出单链表。接着让用户输入要插入的节点的位置和值,调用 insertNode 函数插入节点,并再次调用 printLinkedList 函数输出单链表。最后让用户输入要删除的节点的位置,调用 deleteNode 函数删除节点,并再次调用 printLinkedList 函数输出单链表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)