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



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

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