(亲亲,手把手教你链表,不再头疼,一题搞定所有链表问题!!!!)C语言习题 链表建立,插入,删除,输出

本文介绍了一个使用C/C++实现的学生信息链表管理程序。主要内容包括创建、删除、插入链表节点,以及输出和释放链表的功能。通过示例输入展示了程序的运行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。

输入

输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点

输出

输出的链表

样例输入

1001 100
1002 95
1005 90
1008 76
0 0
1005
1006 98
1009 99

样例输出

1001 100.00
1002 95.00
1006 98.00
1008 76.00
1009 99.00

提示

 

主函数已给定如下,提交时不需要包含下述主函数



/* C代码 */

int main()

{

    struct student *creatlink(void);

    struct student *dellink(struct student *,long);

    struct student *insertlink(struct student *,struct student *);

    void printlink(struct student *);

    void freelink(struct student *);

    struct student *head,stu;

    long del_num;

    head=creatlink();

    scanf("%ld",&del_num);

    head=dellink(head,del_num);

    scanf("%ld%f",&stu.num,&stu.score);

    head=insertlink(head,&stu);

    scanf("%ld%f",&stu.num,&stu.score);

    head=insertlink(head,&stu);

    printlink(head);

    freelink(head);

    return 0;

}



/* C++代码 */



int main()

{

    student *creatlink(void);

    student *dellink(student *,long);

    student *insertlink(student *,student *);

    void printlink(student *);

    void freelink(student *);

    student *head,stu;

    long del_num;

    head=creatlink();

    cin>>del_num;

    head=dellink(head,del_num);

    cin>>stu.num>>stu.score;

    head=insertlink(head,&stu);

    cin>>stu.num>>stu.score;

    head=insertlink(head,&stu);

    cout<<setiosflags(ios::fixed);

    cout<<setprecision(2);

    printlink(head);

    freelink(head);

    return 0;

}

#include<iostream>
#include<stdio.h>
#include<iomanip>///小数输出在此函数库
#include<stdlib.h>///free函数在此库
using namespace std;
struct student
{///根据主函数的提示,确定数据类型
    long num;
    float score;
    struct student *next;///结构体中定义指向结构体
};
student *creatlink()
{///第一个函数,创造链表
    student *p,*q,*head=NULL;
    int a,b;///头结点为空
    while(cin>>a>>b&&a&&b)
    {///输入数据不能同时为零
        p=new student;///创造新节点
        p->num=a;
        p->score=b;///一个节点存储两个数据
        p->next=NULL;///初始下一个节点为空
        if(head==NULL)
            head=p;///如果是第一个创建的节点,则头指针指向该节点
        else///如果不是第一个创建的,将上一个节点的后继指针指向当前节点
            q->next=p;
        q=p;///q也要指向当前节点,因为临时指针p将会指向新节点
    }///返回头结点
    return head;
}
student *dellink(student *head,long deletenum)
{///函数2:删除给定节点
    student *t=head,*q;
    while(t!=NULL)
    {
        if(t->num==deletenum)
        {///找到该点
            q->next=t->next;///前驱节点的下一个节点指向要删节点的下一个节点
            delete(t);///删除节点
            t=q->next;///移动,变成前驱节点的下一个节点
        }
        else
        {///找不到
            q=t;///q也要指向当前节点,因为临时指针t将会指向新节点
            t=t->next;///移动节点t
        }
    }///返回头结点
    return head;
}
student *insertlink(student *head,student *stu)
///后面这个好像是把结构体传进来了鸭。。。
{///函数3.。。。。。。。。。
    student *t=head,*p;
    while(t!=NULL)
    {///如果为最后一个节点或者该节点的下一个节点的值比插入节点的值要点的话
    ///就插入,注意插入的是num和score两个
        if(t->next==NULL||t->next->num>stu->num||t->next->score>stu->score)
        {
            p=new student;///插入节点需要开一个新节点
            p->num=stu->num;
            p->score=stu->score;///插入的值赋给新节点
            p->next=t->next;///插入节点的下一节点指向前驱节点的下一节点
            t->next=p;///前驱节点的下一节点为新节点
            break;///找到了就退出吧
        }
        t=t->next;///节点移动
    }
    return head;///不想说了。。
}
void printlink(student *t)
{///输出链表,遍历
    while(t!=NULL)
    {
        cout<<t->num<<" "<<t->score<<endl;
        t=t->next;
    }
}
void freelink(student *t)
{///把节点一一释放
    while(t!=NULL)
    {
        free(t);
        t=t->next;
    }
}
int main()
{
    student *creatlink(void);
    student *dellink(student *,long);
    student *insertlink(student *,student *);
    void printlink(student *);
    void freelink(student *);
    student *head,stu;
    long del_num;
    head=creatlink();
    cin>>del_num;
    head=dellink(head,del_num);
    cin>>stu.num>>stu.score;
    head=insertlink(head,&stu);
    cin>>stu.num>>stu.score;
    head=insertlink(head,&stu);
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    printlink(head);
    freelink(head);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值