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

// 定义学生结点
struct Student {
    int id;
    char name[20];
    struct Student *next;
};

// 创建新结点
struct Student* createNode(int id, char *name) {
    struct Student *newNode = (struct Student*)malloc(sizeof(struct Student));
    newNode->id = id;
    strcpy(newNode->name, name);
    newNode->next = NULL; // 新结点初始指向 NULL
    return newNode;
}

// 在链表尾部添加结点
void appendNode(struct Student **head, int id, char *name) {
    struct Student *newNode = createNode(id, name);
    
    // 若链表为空，新结点作为头结点
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    
    // 遍历到链表尾部
    struct Student *current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    
    // 连接新结点
    current->next = newNode;
}

// 在指定学号后插入新结点
void insertNode(struct Student *head, int targetId, int newId, char *newName) {
    struct Student *current = head;
    
    // 查找目标结点（学号为targetId的结点）
    while (current != NULL && current->id != targetId) {
        current = current->next;
    }
    
    if (current == NULL) {
        printf("未找到目标结点！\n");
        return;
    }
    
    // 创建新结点
    struct Student *newNode = createNode(newId, newName);
    
    // 插入新结点：新结点指向current的下一个结点
    newNode->next = current->next;
    
    // current指向新结点
    current->next = newNode;
}

// 遍历并显示所有结点数据
void displayList(struct Student *head) {
    struct Student *current = head;
    
    if (current == NULL) {
        printf("链表为空！\n");
        return;
    }
    
    printf("链表数据：\n");
    while (current != NULL) {
        printf("学号：%d，姓名：%s\n", current->id, current->name);
        current = current->next; // 移动到下一个结点
    }
}

int main() {
    struct Student *head = NULL; // 初始化头指针
    
    // 添加3个学生结点（构建链表）
    appendNode(&head, 101, "张三");
    appendNode(&head, 102, "李四");
    appendNode(&head, 103, "王五");
    
    // 在main函数中调用
	insertNode(head, 102, 104, "赵六"); // 在102后插入104
	
    // 在main函数中调用
	displayList(head);
	
    return 0;
}