C语言单链表

文章描述了一个使用C语言实现的单链表数据结构,用于存储班级信息,包括姓名、学号、密码和年龄。文章重点介绍了如何编写一个登录函数,用户输入学号和密码进行验证,以及登录后提供修改姓名、年龄或密码的功能并支持保存或退出操作。
摘要由CSDN通过智能技术生成

设有一条单链表保存了一个班级信息,其中包含:姓名、学号、密码、年龄、请编写一个登入函数,传入这条链表和学号,然后要求输入密码(输入三次错误则程序结束),如果密码正确则登入成功,登入后可以选择修改姓名或年龄或密码,然后进行保存或退出登入。例如: 密码为123456、abcdef,学生姓名为 张三 、李四,年龄为 20,23,学号为1234,1235。登入后可以选择修改姓名或年龄或密码,然后进行保存或退出登入。

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

// 定义链表节点结构体
typedef struct StudentNode {
    char name[50];
    int id;
    char password[20];
    int age;
    struct StudentNode* next;
} StudentNode;

// 创建新节点
StudentNode* createNode(const char* name, int id, const char* password, int age) {
    StudentNode* newNode = (StudentNode*)malloc(sizeof(StudentNode));
    strcpy(newNode->name, name);
    newNode->id = id;
    strcpy(newNode->password, password);
    newNode->age = age;
    newNode->next = NULL;
    return newNode;
}

// 添加节点到链表
void addNodeToList(StudentNode** head, const char* name, int id, const char* password, int age) {
    StudentNode* newNode = createNode(name, id, password, age);
    if (*head == NULL) {
        *head = newNode;
    } else {
        StudentNode* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

/*******************************查找指定学号的学生节点****************************/ 
StudentNode* findNodeById(StudentNode* head, int id) {
    while (head != NULL) {
        if (head->id == id) {
            return head;
        }
        head = head->next;
    }
    return NULL;
}

/************************************修改学生信息***********************************/ 
void modifyStudentInfo(StudentNode* student, const char* newName, int newAge, const char* newPassword) {
    if (newName != NULL) strcpy(student->name, newName);
    if (newAge >= 0) student->age = newAge;
    if (newPassword != NULL) strcpy(student->password, newPassword);
}

/*************************************登录验证*************************************/ 
bool login(StudentNode* head, int id, const char* inputPassword, int maxAttempts) {
    StudentNode* student = findNodeById(head, id);
    int attempts = 0;
    while (attempts < maxAttempts && student != NULL) {
        if (strcmp(student->password, inputPassword) == 0) {
            printf("Login successful!\n");
            return true;
        } else {
            printf("Incorrect password, try again.\n");
            attempts++;
            if (attempts < maxAttempts) {
                printf("Enter your password: ");
                scanf("%s", inputPassword);
                getchar(); // Consume newline character
            }
        }
    }

    if (attempts == maxAttempts) {
        printf("Too many failed attempts, exiting...\n");
    }
    return false;
}

int main() {
    StudentNode* head = NULL;
    addNodeToList(&head, "张三", 1234, "123456", 20);
    addNodeToList(&head, "李四", 1235, "abcdef", 23);

    int id;
    char inputPassword[20];

    printf("Enter your student ID: ");
    scanf("%d", &id);
    getchar(); // Consume newline character

    if (login(head, id, inputPassword, 3)) {
        // 登录成功后,可进行信息修改操作
        printf("You have logged in successfully.\n");

        int choice;
        do {
            printf("\nChoose an option to modify:\n");
            printf("1. Name\n");
            printf("2. Age\n");
            printf("3. Password\n");
            printf("4. Exit and Save Changes\n");
            scanf("%d", &choice);
            getchar(); // Consume newline character

            switch (choice) {
                case 1: {
                    printf("Enter your new name: ");
                    char newName[50];
                    scanf("%s", newName);
                    getchar();
                    modifyStudentInfo(findNodeById(head, id), newName, -1, NULL);
                    break;
                }
                case 2: {
                    int newAge;
                    printf("Enter your new age: ");
                    scanf("%d", &newAge);
                    getchar();
                    modifyStudentInfo(findNodeById(head, id), NULL, newAge, NULL);
                    break;
                }
                case 3: {
                    printf("Enter your new password: ");
                    char newPassword[20];
                    scanf("%s", newPassword);
                    getchar();
                    modifyStudentInfo(findNodeById(head, id), NULL, -1, newPassword);
                    break;
                }
                case 4:
                    printf("Information saved. Logging out...\n");
                    goto exit_and_clean;
                    break;
                default:
                    printf("Invalid choice. Please choose again.\n");
                    break;
            }
        } while (choice != 4);

exit_and_clean:
        // 清理链表(这里仅为示例,实际应用可能需要更复杂的内存管理)
        StudentNode* temp = head;
        while (temp) {
            StudentNode* toDelete = temp;
            temp = temp->next;
            free(toDelete);
        }
        head = NULL;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值