YTU 2430 C语言习题 链表建立,插入,删除,输出

版权声明:转载请附上原文链接哟! https://blog.csdn.net/weixin_44170305/article/details/89919831

风华是一指流砂,苍老是一段年华。

题目描述

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

输入

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

输出

输出的链表

样例输入

copy

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<stdio.h>
#include<stdlib.h>
struct student
{
    long num;
    float score;
    struct student *next;
};
struct student *creatlink()
{
    struct student *p1,*p2,*head=NULL;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num))
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=p1->next=(struct student*)malloc(sizeof(struct student));
    }
    p2->next=NULL;
    return head;
};
struct student *dellink(struct student *a,long b)
{
    struct student *head=a,*p=a,*p2=a;
    while(p!=NULL)
    {
        if(p->num==b)
            p2->next=p->next;
        p2=p;
        p=p->next;
    }
    return head;
};
struct student *insertlink(struct student *a,struct student *b)
{
    struct student *head=a,*p=a,*p2=a,*x;
    x=(struct student*)malloc(sizeof(struct student));
    x->num=b->num;
    x->score=b->score;
    int n=0;
    while(p!=NULL)
    {
        if(p->num>b->num)
        {
            p2->next=x;
            x->next=p;
            n=1;
        }
        p2=p;
        p=p->next;
    }
    if(n==0){
            p2->next=x;
    x->next=NULL;
    }
    return head;
};
void printlink(struct student *a)
{
    struct student *p=a;
    while(p!=NULL)
    {
        printf("%ld %.2f\n",p->num,p->score);
        p=p->next;
    }
}
void freelink(struct student *a)
{
    while(a!=NULL)
    {
        a=a->next;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/89919831