8.12作业

#include "text.h"
/*
 *
 *创建单链表,存储4个学生信息(年龄,分数,姓名)。
1、建立学生结构体数组,存放4个学生信息,循环调用插入函数,建立整表
2、任意位置插入一个新学生。变量e是学生结构体变量。
3、任意位置删除一个学生。
4、单链表逆置后将学生信息输出。
*/



int main(int argc, const char *argv[])
{
  Plink head = NULL;

    // 创建学生结构体数组
    Student students[4] = {
        {"张三", 20, 85},
        {"李四", 22, 90},
        {"王五", 21, 88},
        {"牛六", 19, 80}
    };

    // 循环调用插入函数,建立整表
    for (int i = 3; i >= 0; i--) { // 从最后一个开始插入以保持顺序
        head_insert(&head, &students[i]);
    }

    // 打印链表
    printf("原始链表:\n");
    print_list(head);

    // 创建新学生 Eva
    Student e = {"Eva", 23, 95};

    // 在任意位置插入一个新学生
    insert_at_position(&head, &e, 2);

    // 打印链表
    printf("\n插入新学生后的链表:\n");
    print_list(head);

    // 删除指定位置的学生
    delete_at_position(&head, 3);

    // 打印链表
    printf("\n删除学生后的链表:\n");
    print_list(head);

    // 逆置链表
    reverse_list(&head);

    // 打印链表
    printf("\n逆置后的链表:\n");
    print_list(head);

    // 释放内存
    Link *current = head;
    while (current != NULL) {
        Link *next = current->next;
        free(current->people);
        free(current);
        current = next;
    }
	return 0;
}
#include "text.h"
void head_insert(Plink *head, Student *newStudent) {
    Link *newNode = (Link *)malloc(sizeof(Link));
    if (newNode == NULL) {
        printf("内存分配失败。\n");
        return;
    }
    newNode->people = newStudent;
    newNode->next = *head;
    *head = newNode;
}

void insert_at_position(Plink *head, Student *newStudent, int position) {
    if (position == 0) {
        head_insert(head, newStudent);
        return;
    }

    Link *newNode = (Link *)malloc(sizeof(Link));
    if (newNode == NULL) {
        printf("内存分配失败。\n");
        return;
    }
    newNode->people = newStudent;

    Link *current = *head;
    int currentPosition = 0;
    while (current != NULL && currentPosition < position - 1) {
        current = current->next;
        currentPosition++;
    }

    if (current == NULL) {
        printf("插入位置超出链表长度。\n");
        free(newNode);
        return;
    }

    newNode->next = current->next;
    current->next = newNode;
}

void delete_at_position(Plink *head, int position) {
    if (*head == NULL) {
        printf("链表为空,无法删除。\n");
        return;
    }
    if (position == 0) {
        Link *temp = *head;
        *head = (*head)->next;
        free(temp->people);
        free(temp);
        return;
    }

    Link *current = *head;
    int currentPosition = 0;
    while (current != NULL && currentPosition < position - 1) {
        current = current->next;
        currentPosition++;
    }

    if (current == NULL || current->next == NULL) {
        printf("删除位置超出链表长度。\n");
        return;
    }

    Link *toDelete = current->next;
    current->next = toDelete->next;
    free(toDelete->people);
    free(toDelete);
}

void reverse_list(Plink *head) {
    Link *prev = NULL;
    Link *current = *head;
    Link *next = NULL;

    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    *head = prev;
}

void print_list(const Plink head) {
    const Link *current = head;
    while (current != NULL) {
        printf("姓名: %s, 年龄: %d, 成绩: %d\n", current->people->name, current->people->age, current->people->score);
        current = current->next;
    }
}
#ifndef _TEXT_H_
#define _TEXT_H_
#include <myhead.h>
typedef struct student
{
    char name[20];
    int age;
    int score;
} Student;

// 链表节点结构体
typedef struct tude {
    union {
        int len;
        struct student *people;
    };
    struct tude *next;
} Link, *Plink;

// 插入节点到链表头部
void head_insert(Plink *head, Student *newStudent);

// 在任意位置插入节点
void insert_at_position(Plink *head, Student *newStudent, int position);

// 删除指定位置的节点
void delete_at_position(Plink *head, int position);

// 逆置链表
void reverse_list(Plink *head);

// 打印链表
void print_list(const Plink head);
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值