标题:以下代码有点长,望有耐心的高手帮忙找错
取消只看楼主
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
结帖率:50%
已结贴  问题点数:20 回复次数:4 
以下代码有点长,望有耐心的高手帮忙找错
#include<stdio.h>
#include<stdlib.h>
struct data_array{
    long num;
    int score;
};
typedef struct student{
    long num;
    int score;
    struct student*next;
}NODE;
NODE*insert(NODE*head,NODE*p0)
{
    NODE*p1,*p2=p1=NULL;
    if(head==NULL){
        head=p0;
        p0->next=NULL;
    }
    else{
        p1=head;
        while((p0->num>p1->num)&&(p1->next!=NULL)){
            p2=p1;
            p1=p1->next;
        }
        if(p1==head){
            p0->next=head;
            head=p0;
        }
        else if(p0->num<p1->num){
            p2->next=p0;
            p0->next=p1;
        }
        else{
            p1->next=p0;
            p0->next=NULL;
        }
    }
    return head;
}
NODE*create(struct data_array*array,int n)
{
    int i;
    NODE *p0,*head;
    for(i=0;i<n;i++){
        p0=(NODE*)malloc(sizeof(NODE));
        p0->num=array[i].num;
        p0->score=array[i].score;
        p0->next=NULL;
        
        if(i=0)            
            head=p0;
        else
            insert(head,p0);
    }
    return head;
}
void display(NODE*head)
{
    NODE *p=head;
    do{
        printf("%8ld%4d\n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
    return ;
}
void copy_list(NODE *head1,NODE *head2)
{
    NODE *p1=head1,*p2,*p;
    int temp=1;
    do{
        p2=(NODE*)malloc(sizeof(NODE));
        p2->num=p1->num;
        p2->score=p1->score;
        p2->next=NULL;
        p1=p1->next;
        if(temp==1){
            head2=p=p2;
            temp=0;}
        else{
            p->next=p2;
            p=p2;
        }
        }while(p1!=NULL);
    }
NODE *merge(NODE *head1,NODE *head2)
{

    NODE *p1=head1,*p2=head2;
    static NODE a;
    int temp=1;
    do{
        if(p1->num<p2->num){
            if(temp){
                a=*p1;
                temp=0;
                p1=p1->next;
                continue;
            }
            insert(&a,p1);
            p1=p1->next;
        }
        else if(p1->num==p2->num){
            if(p1->score>=p2->score){
               if(temp){
                 a=*p1;
                 temp=0;
                 p1=p1->next;
                 p2=p2->next;
                 continue;
               }
               insert(&a,p1);
               p1=p1->next;
               p2=p2->next;
            }
            else{
                if(temp){
                 a=*p2;
                 temp=0;
                 p2=p2->next;
                 p1=p1->next;
                 continue;
               }
               insert(&a,p2);
               p2=p2->next;
               p1=p1->next;
            }
        }
        else{
            if(temp){
                a=*p2;
                temp=0;
                p2=p2->next;
                continue;
            }
            insert(&a,p2);
            p2=p2->next;
        }
    }while(p1!=NULL&&p2!=NULL);
    if(p1==NULL)
        do{
            insert(&a,p2);
               p2=p2->next;
        }while(p2!=NULL);
    else if(p2==NULL)
        do{
            insert(&a,p1);
               p1=p1->next;
        }while(p1!=NULL);
    return &a;
}
NODE *del(NODE *head1,NODE *head2)
{
    NODE *p1=head1,*p2=head2,*temp;
    for(;p2!=NULL;p2=p2->next)
        for(;p1!=NULL;temp=p1,p1=p1->next)
            if(p1->num==p2->num){
                if(p1==head1)
                    head1=p1->next;
                else
                    temp->next=p1->next;
                free(p1);
            }
    return head1;
}
int main(void)
{
    struct data_array data_a[5]={{20304,75},{20311,89},{20303,62},{20307,87},{20320,79}};
    struct data_array data_b[]={{20302,65},{20301,99},{20311,87},{20323,88},{20307,92},{20322,83}};
    NODE *a,*b,*a_dup,*b_dup;
    a=create(data_a,5);
    b=create(data_b,6);
    printf("a:   ");
    display(a);
    printf("b:   ");
    display(b);
    copy_list(a,a_dup);
    copy_list(b,b_dup);
    a=merge(a,b);
    printf("a:   ");
    display(a);
    del(b_dup,a_dup);
    printf("b:   ");
    display(b_dup);
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: long 
2011-06-15 12:02
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
得分:0 
回复 2楼 bccn_2012
你用什么编译器?我用vs2010会出现地址引用错误,在insert函数第一个else的第一个while处,就在此处卡住了,主函数所有输出都没有执行,所以必有问题。
2011-06-15 16:18
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
得分:0 
2011-06-16 22:58
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
得分:0 
merge函数功能是将已知的head1,head2两个链表按num升序合并,若num相同则保留score较大的成员
2011-06-17 16:06
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
得分:0 
回复 8楼 cosdos
太感谢了!真不好意思,没把软件目的说出来,还要你猜。这是我的疏忽。
2011-06-19 00:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-342929-1-1.html




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

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