C语言实现链表的创建、销毁、删除、插入和查找(在输入界面进行操作)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct studnode {

    int no;                        // 学号

    char name[8];                  // 姓名

    char sex[2];                   // 性别

    char class[4];                 // 班号

    struct studnode *next;         // 指向下一节点的指针

} StudType;

StudType* CreateList() {

    // 创建头节点

    StudType* head = (StudType*)malloc(sizeof(StudType));

    if (head == NULL) {

        printf("Memory allocation for head failed.\n");

        exit(1);

    }

    head->next = NULL;

    return head;

}

void InsertNode(StudType* listHead, int no, char* name, char* sex, char* class) {

    StudType* newNode = (StudType*)malloc(sizeof(StudType));

    // 检查内存分配是否成功

    if (newNode == NULL) {

        printf("Memory allocation failed.\n");

        exit(1);

    }

    // 填充新节点数据

    newNode->no = no;

    strncpy(newNode->name, name, 7);

    newNode->name[7] = '\0';

    strncpy(newNode->sex, sex, 1);

    newNode->sex[1] = '\0';

    strncpy(newNode->class, class, 3);

    newNode->class[3] = '\0';

    // 插入到链表的开头(头节点之后)

    newNode->next = listHead->next;

    listHead->next = newNode;

}

StudType* FindNode(StudType* listHead, int no) {

    StudType* current = listHead->next;

    while (current != NULL) {

        if (current->no == no) {

            return current;

        }

        current = current->next;

    }

    return NULL;

}

void DeleteNode(StudType* listHead, int no) {

    StudType *current = listHead, *previous = NULL;

    while (current != NULL && current->no != no) {

        previous = current;

        current = current->next;

    }

    if (current != NULL) {

        if (previous != NULL) {

            previous->next= current->next;

        } else {

            listHead->next = current->next;

        }

        free(current);

    } else {

        printf("No student with number %d found.\n", no);

    }

}

void DestroyList(StudType* listHead) {

    StudType* current = listHead;

    while (current != NULL) {

        StudType* next = current->next;

        free(current);

        current = next;

    }

}

void PrintList(StudType* listHead) {

    StudType* current = listHead->next;  // 跳过头节点

    while (current != NULL) {

        printf("No: %d, Name: %s, Sex: %s, Class: %s\n",

            current->no, current->name, current->sex, current->class);

        current = current->next;

    }

}

void InitializeList(StudType* head) {

    InsertNode(head, 1, "张斌", "男", "9901");

    InsertNode(head, 8, "刘丽", "女", "9902");

    InsertNode(head, 34, "李英", "女", "9901");

    InsertNode(head, 20, "陈华", "男", "9902");

    InsertNode(head, 12, "王奇", "男", "9901");

    InsertNode(head, 26, "董强", "男", "9902");

    InsertNode(head, 5, "王萍", "女", "9901");

}

int main() {

    StudType* studentList = CreateList();

    InitializeList(studentList);  // 初始化链表

    // 首次打印链表

    printf("Initial student list:\n");

    PrintList(studentList);

    printf("\n");

    char command;

    int no;

    char name[8], sex[2], class[4];

    printf("Input 'i' to insert, 'd' to delete, 'f' to find, 'p' to print, 'x' to exit.\n");

    while(1) {

        printf("Enter command: ");

        scanf(" %c", &command); // 前面添加空格忽略前面的空白字符

        switch(command) {

            case 'i': // 插入操作

                printf("Enter no, name, sex, class to insert: ");

                scanf("%d %s %s %s", &no, name, sex, class);

                InsertNode(studentList, no, name, sex, class);

                printf("\nUpdated student list after insertion:\n");

                PrintList(studentList);

                printf("\n");

                break;

            case 'd': // 删除操作

                printf("Enter no to delete: ");

                scanf("%d", &no);

                DeleteNode(studentList, no);

                printf("\nUpdated student list after deletion:\n");

                PrintList(studentList);

                printf("\n");

                break;

            case 'f': // 查找操作

                printf("Enter no to find: ");

                scanf("%d", &no);

                StudType* found = FindNode(studentList, no);

                if (found) {

                    printf("Found student: No: %d, Name: %s, Sex: %s, Class: %s\n", found->no, found->name, found->sex, found->class);

                } else {

                    printf("Student with No: %d not found.\n", no);

                }

                printf("\n");

                break;

            case 'p': // 打印操作

                PrintList(studentList);

                printf("\n");

                break;

            case 'x': // 退出操作

                DestroyList(studentList);

                printf("Exiting the program...\n");

                return 0; // 退出程序

            default:

                printf("Invalid command.\n");

        }

    }

    return 0;

}

表格包含的数据元素为学生信息,具体可以根据需求进行修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值