📅  最后修改于: 2023-12-03 15:27:52.207000             🧑  作者: Mango
本程序是一个菜单驱动的程序,用于C语言中双链表上的所有操作。双链表是一种数据结构,它支持在列表的头部和尾部高效地插入和删除元素,并且可以快速地访问列表中的任意元素。
本程序支持以下功能:
我们使用C语言中的结构体来表示双链表节点。每个节点包含三个字段:
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
其中,data
字段存储节点的数据;prev
和next
分别是指向前驱节点和后继节点的指针。
下面是本程序的主程序代码。程序通过一个无限循环来接受用户的输入,并根据用户的选择执行相应的操作。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// 插入节点,返回头节点
struct Node* insert(struct Node* head, int index, int data) {
struct Node* p;
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
if (index == 0) { // 注意特殊情况
node->prev = NULL;
node->next = head;
if (head) head->prev = node;
return node;
}
p = head;
while (index > 1) {
if (p == NULL) { // 注意边界情况
printf("Error: index out of range\n");
return head; // 插入失败,返回原头节点
}
p = p->next;
index--;
}
node->prev = p;
node->next = p->next;
if (p->next) p->next->prev = node;
p->next = node;
return head;
}
// 删除节点,返回头节点
struct Node* delete(struct Node* head, int index) {
struct Node* p = head;
if (index == 0) { // 注意特殊情况
head = head->next;
if (head) head->prev = NULL;
free(p);
return head;
}
while (index > 0) {
if (p == NULL) { // 注意边界情况
printf("Error: index out of range\n");
return head; // 删除失败,返回原头节点
}
p = p->next;
index--;
}
p->prev->next = p->next;
if (p->next) p->next->prev = p->prev;
free(p);
return head;
}
// 搜索元素,返回元素位置
int search(struct Node* head, int data) {
int index = 0;
struct Node* p = head;
while (p) {
if (p->data == data) return index;
p = p->next;
index++;
}
return -1; // 搜索失败
}
// 显示所有元素
void show(struct Node* head) {
struct Node* p = head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
int op, index, data;
struct Node* head = NULL;
while (1) {
printf("1. Create list\n");
printf("2. Insert at head\n");
printf("3. Insert at tail\n");
printf("4. Insert at index\n");
printf("5. Delete at head\n");
printf("6. Delete at tail\n");
printf("7. Delete at index\n");
printf("8. Search element\n");
printf("9. Show list\n");
printf("10. Exit\n");
printf(">>> ");
scanf("%d", &op);
switch (op) {
case 1:
printf("Enter list size: ");
scanf("%d", &index);
printf("Enter list elements: ");
for (int i = 0; i < index; i++) {
scanf("%d", &data);
head = insert(head, i, data);
}
break;
case 2:
printf("Enter element: ");
scanf("%d", &data);
head = insert(head, 0, data);
break;
case 3:
printf("Enter element: ");
scanf("%d", &data);
index = 0;
struct Node* p = head;
while (p && p->next) {
p = p->next;
index++;
}
head = insert(head, index+1, data);
break;
case 4:
printf("Enter index: ");
scanf("%d", &index);
printf("Enter element: ");
scanf("%d", &data);
head = insert(head, index, data);
break;
case 5:
head = delete(head, 0);
break;
case 6:
index = 0;
p = head;
while (p && p->next) {
p = p->next;
index++;
}
head = delete(head, index);
break;
case 7:
printf("Enter index: ");
scanf("%d", &index);
head = delete(head, index);
break;
case 8:
printf("Enter element: ");
scanf("%d", &data);
index = search(head, data);
if (index == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", index);
}
break;
case 9:
show(head);
break;
case 10:
exit(0);
default:
printf("Invalid operation\n");
break;
}
}
return 0;
}
本程序实现了双链表的所有基本操作,并通过菜单驱动的方式提供了用户界面,方便操作。程序避免了内存泄漏的问题,同时对输入数据进行了有效性检查,提高了程序健壮性。程序可扩展性较好,可以方便地添加其他功能。