zoukankan      html  css  js  c++  java
  • YTU 2430: C语言习题 链表建立,插入,删除,输出

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

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 576  解决: 280

    题目描述

    编写一个函数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<stdio.h>
    #include<iostream>
    #include<stdlib.h>
    #include<string.h>
    #include<iomanip>
    using namespace std;
    struct student
    {
        long int 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,*k;
        k=(struct student*)malloc(sizeof(struct student));
        k->num=b->num,k->score=b->score;
        int n=0;
        while(p!=NULL)
        {
            if(p->num>b->num)
            {
                p2->next=k;
                k->next=p;
                n=1;
            }
            p2=p;
            p=p->next;
        }
        if(n==0)p2->next=k,k->next=NULL;
        return head;
    };
    void printlink(struct student *a)
    {
        struct student *p=a;
        while(p!=NULL)
        {
            printf("%ld %.2f
    ",p->num,p->score);
            p=p->next;
        }
    }
    void freelink(struct student *a)
    {
        while(a!=NULL)
        {
            delete(a);
            a=a->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;
    }

  • 相关阅读:
    EZ 2018 1 21 2018noip第五次膜你赛
    POJ 1068&&2632&&1573&&2993&&2996
    POJ 3278&&2049&&3083
    POJ 1328&&2109&&2586
    POJ 2965&&1753
    EZ 2018 01 14 2018noip第四次膜你赛
    LCA的一些算法
    Image Processing and Analysis_15_Image Registration: A Method for Registration of 3-D shapes——1992
    Image Processing and Analysis_15_Image Registration:Image matching as a diffusion process: An analogy with Maxwell's demons——1998
    Signal Processing and Pattern Recognition in Vision_15_RANSAC:Random Sample Consensus——1981
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989451.html
Copyright © 2011-2022 走看看