单链表作业

list.h

ifndef _WORK_H_
#define _WORK_H_
// 定义学生结构体
typedef struct {
    int age;
    int score;
    char name[50];
} Student;

// 链表节点结构体
typedef struct node {
    Student data;
    struct node *next;  // 指向下一个节点的指针
} list, *plist;

// 函数声明
plist gethead();
int insertany(plist l, Student e, int n);
int deletany(plist l, int n);
int nodereverse(plist l);
int nodeoutput(plist l);

#endif

list.c 

#include "work.h"
#include<myhead.h>
// 获取头结点,初始化链表
plist gethead() 
{
    plist p = (plist)malloc(sizeof(list));
    if (p == NULL) {
        printf("申请失败\n");
        return NULL;
    }
    p->next = NULL;  // 初始化指针域为空
    return p;
}

// 在链表任意位置插入节点
int insertany(plist l, Student e, int n) 
{
    if (l == NULL) 
    {
        printf("插入失败\n");
        return -1;
    }
    int i;
    plist t = l;
    for (i = 0; i < n - 1; i++)
     {
        t = t->next;
        if (t == NULL) {
            printf("插入位置超出链表长度\n");
            return -1;
        }
    }
    plist p = (plist)malloc(sizeof(list));
    if (p == NULL) 
    {
        printf("内存分配失败\n");
        return -1;
    }
    p->data = e;

    p->next = t->next;
    t->next = p;

    return 0;
}

// 删除任意位置的节点
int deletany(plist l, int n) 
{
    if (l == NULL || l->next == NULL) 
    {
        printf("单链表为空\n");
        return -1;
    }
    int i;
    plist t = l;
    for (i = 0; i < n - 1; i++) 
    {
        t = t->next;
        if (t->next == NULL) {
            printf("删除位置超出链表长度\n");
            return -1;
        }
    }
    plist q = t->next;
    t->next = t->next->next;
    free(q);
    return 0;
}

// 逆置链表
int nodereverse(plist l) {
    if (l == NULL || l->next == NULL) 
    {
        printf("单链表不存在或者为空\n");
        return -1;
    }
    plist p = NULL;
    plist t = l->next;
    plist q = NULL;

    while (t != NULL) 
    {
        q = t->next;
        t->next = p;
        p = t;
        t = q;
    }
    l->next = p;

    return 0;
}

// 输出链表中所有学生信息
int nodeoutput(plist l) {
    if (l == NULL || l->next == NULL) 
    {
        printf("单链表为空\n");
        return -1;
    }
    plist p = l->next;
    printf("学生信息:\n");
    while (p != NULL) 
    {
        printf("姓名: %s, 年龄: %d, 分数: %d\n", p->data.name, p->data.age, p->data.score);
        p = p->next;
    }
    return 0;
}

mymain.c

#include "work.h"
#include<myhead.h>
int main() {
    // 创建链表头结点
    plist student_list = gethead();

    // 创建学生信息数组
    Student students[4] = {
        {20, 85, "A"},
        {21, 90, "B"},
        {19, 88, "C"},
        {22, 92, "D"}
    };
    for (int i = 0; i < 4; i++) 
    {
        Student new_student;
        printf("输入第 %d 个学生的信息\n", i + 1);
        printf("姓名: ");
        scanf("%s", new_student.name);
        printf("年龄: ");
        scanf("%d", &new_student.age);
        printf("分数: ");
        scanf("%d", &new_student.score);

        // 插入到链表末尾
        insertany(student_list, new_student, i + 1);
    }

    // 循环插入学生信息,建立链表
    for (int i = 0; i < 4; i++) {
        insertany(student_list, students[i], i + 1);
    }

    // 输出初始学生信息
    printf("初始学生信息:\n");
    nodeoutput(student_list);

    // 插入新学生
    Student new_student = {23, 91, "E"};
    int insert_position = 3;  // 插入位置
    insertany(student_list, new_student, insert_position);

    // 输出插入后的学生信息
    printf("\n插入新学生后的信息:\n");
    nodeoutput(student_list);

    // 删除一个学生
    int delete_position = 2;  // 删除位置
    deletany(student_list, delete_position);

    // 输出删除后的学生信息
    printf("\n删除后的学生信息:\n");
    nodeoutput(student_list);

    // 逆置链表
    nodereverse(student_list);

    // 输出逆置后的学生信息
    printf("\n逆置后的学生信息:\n");
    nodeoutput(student_list);

    return 0;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值