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

来源:互联网 发布:淘宝虚拟物品货源 编辑:程序博客网 时间:2024/05/21 09:39

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

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

题目描述

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

输入

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

输出

输出的链表

样例输入

1001 1001002 951005 901008 760 010051006 981009 99

样例输出

1001 100.001002 95.001006 98.001008 76.001009 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\n",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;}

1 0
原创粉丝点击