若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

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

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,315

题目描述

编写一个函数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

参考代码

#include <stdio.h>
#include <stdlib.h>
struct student 
{
    long num;
    float score;
    struct student *next;
}
;
struct student *creatlink(void)       //建立链表的函数 
{
    struct student *head;
    struct student *p1,*p2;
    int n=0;
    p1=p2=(struct student*)malloc(sizeof(struct student));
    //开辟一个新单元,并使p1,p2指向它
    scanf("%ld%f",&p1->num,&p1->score);
    head=NULL;
    while(p1->num!=0) 
    {
        n=n+1;
        if(n==1) head=p1; else p2->next=p1;
        p2=p1;
        p1=(struct student*)malloc(sizeof(struct student));
        scanf("%ld%f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return(head);
}
struct student *dellink(struct student *head,long num)   //删除结的函数 
{
    struct student *p1,*p2;
    if (head==NULL)                    //是空表 
    {
        //cout<<"list null!"<<endl;
        return(head);
    }
    p1=head;
    //使p1指向第一个结点
    while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点 
    {
        p2=p1;
        //p1后移一个结点
        p1=p1->next;
    }
    if(num==p1->num)                        //找到了 
    {
        if(p1==head) head=p1->next;
        //若p1指向的是首结点,把第二个结点地址赋予head else p2->next=p1->next;
        //否则将下一结点地址赋给前一结点地址
        //cout<<"delete:"<<num<<endl;
        //        n=n-1;
    }
    //else cout<<"cannot find "<<num;     //找不到该结点
    return(head);
}
struct student *insertlink(struct student *head,struct student *stud)  //插入结点的函数 
{
    struct student *p0,*p1,*p2;
    p1=head;
    //使p1指向第一个结点
    //p0=stud;                          //指向要插入的结点
    p0 =(struct student*)malloc(sizeof(struct student));
    //edit by lyh
    *p0 = *stud;
    // 2013.11.7
    if(head==NULL)                    //原来的链表是空表 
    {
        head=p0;
        //使p0指向的结点作为头结点
        p0->next=NULL;
    } else 
    {
        while((p0->num>p1->num) && (p1->next!=NULL)) 
        {
            p2=p1;
            //使p2指向刚才p1指向的结点
            p1=p1->next;
        }
        //p1后移一个结点
        if(p0->num<=p1->num) 
        {
            if(head==p1) head=p0;
            //插到原来第一个结点之前 else p2->next=p0;
            //插到p2指向的结点之后*/
            p0->next=p1;
        } else 
        {
            p1->next=p0;
            //插到最后的结点之后
            p0->next=NULL;
        }
    }
    //    n=n+1;                         //结点数加1
    return (head);
}
void printlink(struct student *head)         //输出链表的函数 
{
    struct student *p;
    //cout<<"Now,These "<<n<<" records are:"<<endl;
    p=head;
    if(head!=NULL)
            do 
    {
        printf("%ld %.2fn",p->num,p->score);
            p=p->next;
        }
        while(p!=NULL);
}
void freelink(struct student *head)         //释放链表
{
    struct student *p,*q;
    //cout<<"Now,These "<<n<<" records are:"<<endl;
    p=head;
    if(head!=NULL)
        do
        {
            q =p ;
            p=p->next;
            free(q);
        }
        while(p!=NULL);
}
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;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip Dreams孤独患者 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist