📜  C语言中单向链表上所有操作的菜单驱动程序(1)

📅  最后修改于: 2023-12-03 15:00:13.697000             🧑  作者: Mango

C语言中单向链表上所有操作的菜单驱动程序

单向链表是一种常见的数据结构,可以应用于许多实际问题中。本文将介绍C语言中单向链表上所有操作的菜单驱动程序。

什么是单向链表?

单向链表是由节点构成的链式数据结构,每个节点包含两部分:数据和指向下一个节点的指针。链表的起点称为头结点,终点称为尾节点,尾节点指针为空。

单向链表的操作

单向链表支持的操作包括:

  • 创建链表
  • 插入节点
  • 删除节点
  • 修改节点
  • 遍历链表
  • 查找节点
  • 销毁链表

下面是C语言中单向链表上所有操作的菜单驱动程序的实现。

#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *create_list();
int show_menu();
void insert_node(struct node *head);
void delete_node(struct node *head);
void modify_node(struct node *head);
void traverse_list(struct node *head);
void find_node(struct node *head);
void destroy_list(struct node *head);

int main()
{
    int choice;
    struct node *head = create_list();

    do {
        choice = show_menu();
        switch (choice) {
            case 1:
                insert_node(head);
                break;
            case 2:
                delete_node(head);
                break;
            case 3:
                modify_node(head);
                break;
            case 4:
                traverse_list(head);
                break;
            case 5:
                find_node(head);
                break;
            case 6:
                destroy_list(head);
                break;
            case 7:
                printf("Exit\n");
                break;
            default:
                printf("Invalid choice\n");
        }
    } while (choice != 7);

    return 0;
}

// 创建链表
struct node *create_list()
{
    struct node *head = (struct node *)malloc(sizeof(struct node));
    if (head == NULL) {
        printf("Memory allocation failed\n");
        exit(1);
    }
    head->next = NULL;
    return head;
}

// 显示菜单
int show_menu()
{
    int choice;
    printf("\nOperations:\n");
    printf("1. Insert a node\n");
    printf("2. Delete a node\n");
    printf("3. Modify a node\n");
    printf("4. Traverse the list\n");
    printf("5. Find a node\n");
    printf("6. Destroy the list\n");
    printf("7. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    return choice;
}

// 插入节点
void insert_node(struct node *head)
{
    int position, value;
    printf("Enter the position to insert a node: ");
    scanf("%d", &position);
    printf("Enter the value of the node: ");
    scanf("%d", &value);
    struct node *p = head;
    for (int i = 1; i < position; i++) {
        if (p->next == NULL) {
            printf("Invalid position\n");
            return;
        }
        p = p->next;
    }
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
    if (new_node == NULL) {
        printf("Memory allocation failed\n");
        exit(1);
    }
    new_node->data = value;
    new_node->next = p->next;
    p->next = new_node;
}

// 删除节点
void delete_node(struct node *head)
{
    int position;
    printf("Enter the position to delete a node: ");
    scanf("%d", &position);
    struct node *p = head;
    for (int i = 1; i < position; i++) {
        if (p->next == NULL) {
            printf("Invalid position\n");
            return;
        }
        p = p->next;
    }
    struct node *q = p->next;
    p->next = q->next;
    free(q);
}

// 修改节点
void modify_node(struct node *head)
{
    int position, value;
    printf("Enter the position to modify a node: ");
    scanf("%d", &position);
    printf("Enter the new value of the node: ");
    scanf("%d", &value);
    struct node *p = head;
    for (int i = 1; i <= position; i++) {
        if (p->next == NULL) {
            printf("Invalid position\n");
            return;
        }
        p = p->next;
    }
    p->data = value;
}

// 遍历链表
void traverse_list(struct node *head)
{
    struct node *p = head->next;
    if (p == NULL) {
        printf("The list is empty\n");
        return;
    }
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

// 查找节点
void find_node(struct node *head)
{
    int value;
    printf("Enter the value to find a node: ");
    scanf("%d", &value);
    struct node *p = head->next;
    int position = 1;
    while (p != NULL) {
        if (p->data == value) {
            printf("The position of the node is %d\n", position);
            return;
        }
        p = p->next;
        position++;
    }
    printf("The value is not in the list\n");
}

// 销毁链表
void destroy_list(struct node *head)
{
    struct node *p = head->next;
    while (p != NULL) {
        struct node *q = p;
        p = p->next;
        free(q);
    }
    head->next = NULL;
    printf("The list is destroyed\n");
}

以上代码基本实现了所有单向链表的操作,并且在主函数中通过菜单驱动程序实现了交互式操作。

总结

本文介绍了C语言中单向链表上所有操作的菜单驱动程序,并通过实现了一个简单的交互式界面,使得操作更加人性化,并且方便开发和测试。如果您对单向链表还不熟悉,可以阅读本项目中的其他文章进一步了解单向链表。