| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 352 人关注过本帖
标题:大一c语言链表错误怎么改?
只看楼主 加入收藏
a951122
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-12-22
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
大一c语言链表错误怎么改?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define LEN sizeof(struct student)

struct student
{
    long num;
    char name[20];
    int score;
    struct student*next;
};



struct student*creat();
void display(struct student*head);
struct student*insert(struct student*head,struct student*newp);
struct student*deletep(struct student*head,long num);
choose();
void average(struct student*head);



void main()
{
    struct student*creat();
    void display(struct student*head);
    printf("the menu choose:1 inset  2 delete  3 print");
    choose();
    average(struct student*creat());
}
   






int count=0;
struct student*creat()
{
    struct student*head=NULL,*newp,*tail;

    newp=tail=(struct student*)malloc(LEN);

    printf("please input the data:\n");
    scanf("%ld%s%d",&newp->num,newp->name,&newp->score);

    while(newp->num!=0)
    {
        count++;
        if(count==1)
        head=newp;
        else
            tail->next=newp;
        tail=newp;
        newp=(struct student*)malloc(LEN);
        scanf("%ld%s%d",&newp->num,newp->name,&newp->score);

    }
    tail->next=NULL;
    return(head);
}





void display(struct student*head)
{
    struct student*p=head;
    while(p!=NULL)
    {
        printf("%ld\t%s\t%d\n",p->num,p->name,p->score);
        p=p->next;
    }
}


struct student*insert(struct student*head,struct student*newp)
{
     struct student*p,*q;
     p=q=head;

     if(head==NULL)
     {
         head=newp;
         newp->next=NULL;
     }
     else
     {
         while((newp->num>p->num)&&(p->num!=NULL))
         {
             q=p;
             p=p->next;
         }
         if(newp->num<=p->num)
         {
             newp->next=p;
             if(head==p)
                 head=newp;
             else
                 q->next=newp;
         }
         else
         {
             p->next=newp;
             newp->next=NULL;
         }
     }
     count++;
     return(head);
}



struct student*deletep(struct student*head,long num)
{
    struct student*p,*q;
    p=q=head;
    while(p->num!=num&&p->next!=NULL)
    {
        q=p;
        p=p->next;
    }

    if(p->num==num)
    {
        if(p==head)
            head=p->next;
        else
            q->next=p->next;
        free(p);
        count--;
    }
    else
        printf("%ld cannot found\n",num);
    return(head);

}


choose()
{
    int i;
    pirnf("your choice");
    scanf("%d",&i);
    switch(i)
    {
    case 1 :
        struct student*insert(struct student*head,struct student*newp);
        break;
    case 2 :
        struct student*deletep(struct student*head,long num);
        break;
    case 3 :void display(struct student*head);
        break;
    }
}




void average(struct student*head)
{
    int sum=0;
    int ave=0;
    int n;
    struct student*p;
    p=head;
    while(p->next!=NULL)
        sum=sum+p->num;
    printf("the number of date\n");
    scanf("%d",&n);
    ave=sum/n;
    printf("%d",&ave);
}

求大神帮忙改一下,有错误
顺便将其排序
搜索更多相关主题的帖子: average display include insert c语言 
2013-12-27 22:25
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
先看正确的怎么写

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-12-29 12:49
快速回复:大一c语言链表错误怎么改?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021707 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved