鱼C论坛

 找回密码
 立即注册
查看: 2679|回复: 2

不知道为什么这个程序有错误啊!求解啊

[复制链接]
发表于 2012-2-21 18:48:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct stu)
#define NULL 0
struct stu
{
     int num;      //学号
         char name[20];//名字
         char sex;     //性别
         float score;  //成绩
         float height;//身高
         float weight;//体重
         struct stu *next;
};
int n;
struct stu *charu(struct stu *head,struct stu *pn);//插入链表
struct stu *creat();//创建一个链表
void print(struct stu *head);//输出函数
struct stu *shanchu(struct stu *head,int del);//删除链表节点函数

void main()
{
     struct  stu *head,*p1;
         int del;
         printf("请输入学生的:学号,名字,性别,成绩,身高,体重\n");
         head=creat();//创建一个链表
         print(head);//
         printf("请输入你要删除数据:\n");
         scanf("%d",&del);
         while(del!=0)
         {
             head=shanchu(head ,del);
                 print(head);
                 printf("请输入你要删除数据:\n");
         scanf("%d",&del);
         }
         printf("请输入你要插入数据\n");
         p1=(struct stu *)malloc(LEN);
         scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);
         while(p1->num!=0&&p1->height!=0&&p1->weight)
         {
             head=charu(head,p1);
                 print(head);
                 printf("请输入你要插入节点\n");
         p1=(struct stu *)malloc(LEN);
                 scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);;
         }
}

struct stu *creat()//创建一个链表
{
    struct stu *p1,*p2,*head;
        p1=p2=(struct stu *)malloc(LEN);
        n=0;
        scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);
        head=NULL;
        while(p1->num!=0&&p1->height!=0&&p1->weight!=0)
        {   
                 n=n+1;
             if(n==1)
                         head=p1;
                 else
                         p2->next=p1;
                 p2=p1;
                 p1=(struct stu *)malloc(LEN);
         scanf("%d,%s,%c,%f,%f,%f",&p1->num,&p1->name,&p1->sex,&p1->score,&p1->height,&p1->weight);                 
        }
        p2->next=NULL;
        return head;
}

void print(struct stu *head)//打印函数的输出
{
    struct stu *p1;
        p1=head;
        printf("\nNow ,These %d records are :\n",n);
        while(p1!=NULL)
        {
            printf("%d,%5s,%5c,%5f,%5f,%5f\n",p1->num,p1->name,p1->sex,p1->score,p1->height,p1->weight);
                p1=p1->next;
        }
}

struct stu *shanchu(struct stu *head,int del)//删除链表节点函数
{
    struct stu *p1,*p2;
        p1=head;
        if(head==NULL)
        {
                printf("找不到该数\n");
                goto end;
        }
        while(p1->next!=NULL&&del!=p1->num)
        {
            p2=p1;
                p1=p1->next;
        }
        if(del==p1->num)
        {
            if(head==p1)
                        head=p1->next;
                else
                        p2->next=p1->next;
                n=n-1;
                printf("delete:%d,%d",del,p1->num);
        }
        else
                printf("找不到你要删除的数据\n");
end:
        return head;
}

struct stu *charu(struct stu *head,struct stu *pn)
{
     struct stu *p1,*p2;
         p1=head;
         if(head==NULL)
         {
            head=pn;
                pn->next=NULL;
         }
         if(pn->num<=head->num)
         {
             pn->next=head;
                 head=pn;         
         }
         while(p1->next!=0&&pn->num<=p1->num)
         {
                 p2=p1;
                 p1=p1->next;
         }
         if(pn->num<=p1->num)
         {
             p1->next=pn;
                 pn->next=NULL;
         }
         else
         {
                 pn->next=p1;
             p2->next=pn;
         }
         return head;
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-21 19:13:21 | 显示全部楼层
靠   我什么时候也能自己编个这样的,就算是错的也成啊。。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-2-21 23:33:58 | 显示全部楼层
哎,我看得书编的了,现在有错误找不出了啊
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-11 13:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表