| 
 | 
 
 
 楼主 |
发表于 2015-8-6 09:02:24
|
显示全部楼层
 
 
 
我自己解决了。。。。 
主要问题是主函数中要没有将返回的指针接回来。。(应该可以这么表述吧) 
还有就是出现了一些逻辑问题。 
以下是修改后的源代码: 
 
#include<stdio.h> 
#include<malloc.h> 
 
#define NULL 0 
#define LEN sizeof(struct student) 
 
struct student 
{ 
        int num; 
        float score; 
        struct student *next; 
}; 
int n; /* 统计数据 */ 
 
 
struct student *scan(struct student *head) /* 输入函数 */ 
{ 
        struct student *p1,*p2; 
        p1=p2=(struct student *) malloc(LEN); 
        n=0; 
        printf("请输入学号:"); 
        scanf("%d",&p1->num); 
        printf("请输入成绩:"); 
        scanf("%f",&p1->score); 
        while(p1->num!=0) 
        { 
                n++; 
                if(1==n) 
                { 
                        head=p1; 
                } 
                else 
                { 
                        p2->next=p1; 
                } 
                p2=p1; 
                p1=(struct student *) malloc(LEN); 
                printf("请输入学号:"); 
                scanf("%d",&p1->num); 
                printf("请输入成绩:"); 
                scanf("%f",&p1->score); 
        } 
        p2->next=NULL; 
        printf("\n\n共有%d个学生!\n\n",n); 
        return head; 
} 
 
 
struct student *del(struct student *head)  /* 删除函数 */ 
{ 
        struct student *p1,*p2; 
        int num; 
        printf("请输入要删除的数据:"); 
        scanf("%d",&num); 
        if(head==NULL) 
        { 
                printf("这是一个空表!\n"); 
                return head; 
        } 
        p1=head; 
        while(num!=p1->num&&p1->next!=NULL) 
        { 
                p2=p1; 
                p1=p1->next; 
        } 
        if(num==p1->num) 
        { 
                if(p1==head) 
                { 
                        head=p1->next; 
                } 
                else  
                { 
                        p2->next=p1->next; 
                } 
        } 
        else 
        { 
                printf("没找到要删除的数据!\n"); 
        } 
        n=n-1; 
        printf("\n\n共有%d个学生!\n\n",n); 
        return head; 
}  
 
 
struct student *join(struct student *head)  /* 插入函数 */ 
{ 
        struct student *p0,*p1,*p2;         
        printf("请输入要插入的信息:\n"); 
        p0=(struct student *) malloc(LEN); 
        printf("请输入学号:"); 
        scanf("%d",&p0->num); 
        printf("请输入成绩:"); 
        scanf("%f",&p0->score); 
        p1=head; 
        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(p1==head) 
                                { 
                                        head=p0; 
                                        p0->next=p1->next; 
                                } 
                                else 
                                { 
                                        p2->next=p0; 
                                        p0->next=p1->next; 
                                } 
                                 
                        } 
                        else 
                        { 
                                p1->next=p0; 
                                p0->next=NULL; 
                        } 
                 
        } 
         
        n=n+1; 
        printf("\n\n共有%d个学生!\n\n",n); 
        return head; 
} 
 
 
struct student pri(struct student *head) /* 输出函数 */ 
{ 
        struct student *p1; 
        int k=1; 
        p1=head; 
        if(head==NULL) 
        { 
                printf("这是一个空表!"); 
        } 
        else 
        { 
                while(k) 
                { 
                        printf("学号:%5d  成绩:%5.2f\n",p1->num,p1->score); 
                        p1=p1->next; 
                        if(p1->next==NULL) 
                        { 
                                printf("学号:%5d  成绩:%5.2f\n",p1->num,p1->score); 
                                k=0; 
                        } 
                } 
        } 
        printf("\n\n共有%d个学生!\n\n",n); 
} 
 
 
void main() 
{ 
        struct student *head; 
        head=scan(head); 
        pri(head); 
        head=del(head); 
        pri(head); 
    head=join(head); 
        pri(head);         
         
} 
哪里可以优化的,希望大神指点。 |   
 
 
 
 |