鱼C论坛

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

谁可以告诉我insert函数错在哪里

[复制链接]
发表于 2012-5-4 22:53:53 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>

#define LEN sizeof (struct student)

struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert(struct student *head,struct student *stu_2);

void print(struct student *head);

struct student
{
    int num;
        float score;
        struct student *next;
};

int n;

void main()
{
        struct student *stu,*p,stu_2;
        int n;

        stu=creat();
        p=stu;
        print(p);

        printf("please enter the num to delete:");
        scanf("%d",&n);
    print(del(p,n) );

        printf("\nplease input the num to insert:\n");
        scanf("%d",stu_2.num);
        printf("please input the score :");
        scanf("%f",&stu_2.score);

        p=insert(stu,&stu_2);
        print(p);

        printf("\n\n");
        system("pause");

}

struct student *creat()
{
        struct student *head;
        struct student *p1,*p2;

        p1=p2=(struct student *)malloc(LEN);
    printf("输入9999结束\n\n");
        printf("please enter the num:");
        scanf("%d",&p1->num);
    while(999>p1->num||p1->num>10000)
{        printf("学号为4位,请重新输入\n");
    system("pause");
        //system("cls");
    printf("please enter the num:");
    scanf("%d",&p1->num);
        if(p1->num>999&&p1->num<10000)
                break;
        system("cls");
       
}
        printf("please enter the score:");
        scanf("%f",&p1->score);

        head=NULL;
        n=0;

        while(p1->num!=9999)
        {
                n++;
                if(1==n)
                {
                        head=p1;
                }else
                {
                        p2->next=p1;
                }
                p2=p1;
                p1=(struct student *)malloc(LEN);
                printf("\nplease enter the num :");
                scanf("%d",&p1->num);
                while(999>p1->num||p1->num>10000)
{        printf("学号为4位,请重新输入\n");
    system("pause");
        //system("cls");
    printf("please enter the num:");
    scanf("%d",&p1->num);
        if(p1->num>999&&p1->num<10000)
                break;
        system("cls");
       
}
                printf("please enter the score :");
                scanf("%f",&p1->score);
        }
        p2->next=NULL;
        return head;
}

void print(struct student *head)
{
        struct student *p;
        printf("\n there are(is) %d record(s)\n\n",n);

        p=head;
        if(head!=NULL)
        {
                do
                {
                        printf("学号  %d 的成绩是: %.2f\n",p->num,p->score);
                        p=p->next;
                }while(p!=NULL);
        }


}


struct student *del(struct student *h ,int num)
{
        struct student *p1,*p2;
    struct student *head=h;
        if(NULL==head)
        {
                printf("\nthis list is NULL\n");
               
                  goto END;
        }
        p1=head;
        while(p1->num!=num&&p1->next!=NULL)
        {
                p2=p1;
                p1=p1->next;
        }
        if(num==p1->num)
        {
                if(p1==head)
                {
                        head=p1->next;}
                else
                {
                        p2->next=p1->next;
                }
                printf("\ndelete NO : %d succeed!\n",num);
                        n=n-1;
        }
        else
        {
                printf("%d not been found !\n",num);
        }
END:
        return head;

}

struct student *insert(struct student *head,struct student *stu_2)
{
        struct student *p0,*p1,*p2;
        p1=head;
        p0=stu_2;
        if(head == NULL)
        {   head=p0;
                p0->next=NULL;}
        else
        {
                while((p0->num > p1->num)&&(p1->next!=NULL))
                {
                        p2=p1;
                        p1=p1->next;
                }

                if(p0->num <= p1->num)
                {
                        if(head == p1)
                        {
                                head=p0;
                }
                else
                {
                        p2->next = p0;
                }
                p0->next=p1;

                }
    else
        {
                p1->next=p0;
                p0->next=NULL;
        }
  }

    n=n+1;

    return head;
}
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-5-4 22:54:46 | 显示全部楼层
我是对应打上去的:'(:'(
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-5-6 17:39:10 | 显示全部楼层
  是调用的时候才去声明,运算的吧,,,
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-13 05:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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