Nefu 锐格c实验10

5830 根据读入的学生信息,利用尾插法创建链表,多组输入数据

#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
    int sno;
    char str[20];
    double s1;
    double s2;
    double s3;
    double avg;
    double sum;
    struct lnode *next;
}LNode,*LinkList;
void create(LinkList H,int n)
{
    int i;
    LNode *r;
    r=H;
    LNode *s;
    for(i=0;i<n;i++)
    {
       s=(LNode *)malloc(sizeof(LNode));
       scanf("%d",&s->sno);
       scanf("%s",s->str);
       scanf("%lf",&s->s1);
       scanf("%lf",&s->s2);
       scanf("%lf",&s->s3);
       s->sum=(s->s1+s->s2+s->s3);
       s->avg=s->sum/3;
       s->next=r->next;
       r->next=s;
       r=s;
    }
    r->next=NULL;
}
void output(LinkList H)
{
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        p=p->next;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        LinkList H;
        H=(LNode *)malloc(sizeof(LNode));
        create(H,n);
        output(H);
    }
    return 0;
}

5831 利用第一题创建的链表,写一个按学号查找函数,没有找到则输出0

#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
    int sno;
    char str[20];
    double s1;
    double s2;
    double s3;
    double avg;
    double sum;
    struct lnode *next;
}LNode,*LinkList;
void create(LinkList H,int n)
{
    int i;
    LNode *r;
    r=H;
    LNode *s;
    for(i=0;i<n;i++)
    {
       s=(LNode *)malloc(sizeof(LNode));
       scanf("%d",&s->sno);
       scanf("%s",s->str);
       scanf("%lf",&s->s1);
       scanf("%lf",&s->s2);
       scanf("%lf",&s->s3);
       s->sum=(s->s1+s->s2+s->s3);
       s->avg=s->sum/3;
       s->next=r->next;
       r->next=s;
       r=s;
    }
    r->next=NULL;
}
void output(LinkList H)
{
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        p=p->next;
    }
}
void search(LinkList H,int no)
{
    int num=0;
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        if(p->sno==no)
        {
            num++;
            printf("%d  ",num);
            printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        break;
        }
        else
        {
            num++;
            p=p->next;
        }
    }
    if(p==NULL)
    {
        printf("0\n");
    }
}
void output2(LinkList H,LNode *s)
{
    LNode *p;
    p=H->next;
    if(s==NULL)
    {
        printf("0\n");
        return;
    }
    while(p!=NULL&&s)
    {

    }
}
int main()
{
    int n;
    int index;
    while(scanf("%d",&n)!=EOF)
    {
        LinkList H;
        H=(LNode *)malloc(sizeof(LNode));
        create(H,n);
        scanf("%d",&index);
        search(H,index);
        //output(H);
    }
    return 0;
}

/*
5
20140101 LiMing 85 90 92
20140202 ZhaoLi 98 78 88
20140013 QianEn 92.5 85.6 78.5
20140404 TianYa 88.5 68.6 94
20140015 LuYao 89.4 86.5 88
*/

5832 尾插法建立链表,向创建完且学号有序的链表中插入一个新学生信息,插入后链表仍然按照学号有序排列

#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
    int sno;
    char str[20];
    double s1;
    double s2;
    double s3;
    double avg;
    double sum;
    struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,int sa)
{
    LNode *p,*pre;
    p=H->next;pre=H;
    while(p!=NULL&&p->sno<sa)
    {
        pre=p;
        p=p->next;
    }
    LNode *s;
    s=(LNode *)malloc(sizeof(LNode));
    s->sno=sa;
    scanf("%s",s->str);
    scanf("%lf",&s->s1);
    scanf("%lf",&s->s2);
    scanf("%lf",&s->s3);
    s->sum=(s->s1+s->s2+s->s3);
    s->avg=s->sum/3;
    s->next=pre->next;
    pre->next=s;
}
void create(LinkList H,int n)
{
    int i;
    LNode *r;
    r=H;
    LNode *s;
    for(i=0;i<n;i++)
    {
       s=(LNode *)malloc(sizeof(LNode));
       scanf("%d",&s->sno);
       scanf("%s",s->str);
       scanf("%lf",&s->s1);
       scanf("%lf",&s->s2);
       scanf("%lf",&s->s3);
       s->sum=(s->s1+s->s2+s->s3);
       s->avg=s->sum/3;
       s->next=r->next;
       r->next=s;
       r=s;
    }
    r->next=NULL;
}
void output(LinkList H)
{
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        p=p->next;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int sno;
        LinkList H;
        H=(LNode *)malloc(sizeof(LNode));
        create(H,n);
        scanf("%d",&sno);
        yxinsert(H,sno);
        output(H);
    }
    return 0;
}

/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/

5833 在第一题创建完的链表中按照学号删除一个学生信息(学号唯一)

#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
    int sno;
    char str[20];
    double s1;
    double s2;
    double s3;
    double avg;
    double sum;
    struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,int sa)
{
    LNode *p,*pre;
    p=H->next;pre=H;
    while(p!=NULL&&p->sno<sa)
    {
        pre=p;
        p=p->next;
    }
    LNode *s;
    s=(LNode *)malloc(sizeof(LNode));
    s->sno=sa;
    scanf("%s",s->str);
    scanf("%lf",&s->s1);
    scanf("%lf",&s->s2);
    scanf("%lf",&s->s3);
    s->sum=(s->s1+s->s2+s->s3);
    s->avg=s->sum/3;
    s->next=pre->next;
    pre->next=s;
}
void create(LinkList H,int n)
{
    int i;
    LNode *r;
    r=H;
    LNode *s;
    for(i=0;i<n;i++)
    {
       s=(LNode *)malloc(sizeof(LNode));
       scanf("%d",&s->sno);
       scanf("%s",s->str);
       scanf("%lf",&s->s1);
       scanf("%lf",&s->s2);
       scanf("%lf",&s->s3);
       s->sum=(s->s1+s->s2+s->s3);
       s->avg=s->sum/3;
       s->next=r->next;
       r->next=s;
       r=s;
    }
    r->next=NULL;
}
int delete(LinkList H,int sa)
{
    LNode *p,*pre;
    p=H->next;pre=H;
    while(p!=NULL&&p->sno!=sa)
    {
        pre=p;
        p=p->next;
    }
    if(p==NULL)
    {
        return 0;
    }
    else
    {
        pre->next=p->next;
        free(p);
        return 1;
    }
}
void output(LinkList H)
{
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        p=p->next;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int sno;
        LinkList H;
        H=(LNode *)malloc(sizeof(LNode));
        create(H,n);
        scanf("%d",&sno);
        int l;
        //yxinsert(H,sno);
        l=delete(H,sno);
        if(l)
        {
            output(H);
        }
        else
        {
            printf("Error\n");
        }
    }
    return 0;
}

/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/

5834 也是建立有序链表和实验9类似,只不过链表里的结点多了一些信息

#include<stdio.h>
#include<stdlib.h>
#define flag -1
typedef struct lnode
{
    int sno;
    char str[20];
    double s1;
    double s2;
    double s3;
    double avg;
    double sum;
    struct lnode *next;
}LNode,*LinkList;
void yxinsert(LinkList H,LNode *s)
{
    LNode *p,*pre;
    p=H->next;pre=H;
    while(p!=NULL&&p->sum>s->sum)
    {
        pre=p;
        p=p->next;
    }
    s->next=pre->next;
    pre->next=s;
}
void create(LinkList H,int n)
{
    int i;
    int sno;char s[20];double s1,s2,s3,avg,sum;
    for(i=0;i<n;i++)
    {
      LNode *s;
      s=(LNode *)malloc(sizeof(LNode));
      scanf("%d",&s->sno);
      scanf("%s",s->str);
      scanf("%lf",&s->s1);
      scanf("%lf",&s->s2);
      scanf("%lf",&s->s3);
      s->sum=(s->s1+s->s2+s->s3);
      s->avg=s->sum/3;
      yxinsert(H,s);
    }
}
int delete(LinkList H,int sa)
{
    LNode *p,*pre;
    p=H->next;pre=H;
    while(p!=NULL&&p->sno!=sa)
    {
        pre=p;
        p=p->next;
    }
    if(p==NULL)
    {
        return 0;
    }
    else
    {
        pre->next=p->next;
        free(p);
        return 1;
    }
}
void output(LinkList H)
{
    LNode *p;
    p=H->next;
    while(p!=NULL)
    {
        printf("%d  ",p->sno);
        printf("%s  ",p->str);
        printf("%.2lf  ",p->s1);
        printf("%.2lf  ",p->s2);
        printf("%.2lf  ",p->s3);
        printf("%.2lf  ",p->avg);
        printf("%.2lf\n",p->sum);
        p=p->next;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int sno;
        LinkList H;
        H=(LNode *)malloc(sizeof(LNode));
        H->next=NULL;
        create(H,n);
        output(H);
    }
    return 0;
}

/*
3
1001 Wang 90 80 70
1003 Li 60 50 40
1005 Zhang 90 90 90
1004 QQQ 33 44 55
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>