| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 546 人关注过本帖
标题:以下代码有点长,望有耐心的高手帮忙找错
只看楼主 加入收藏
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:8 
以下代码有点长,望有耐心的高手帮忙找错
#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
bccn_2012
Rank: 6Rank: 6
等 级:侠之大者
帖 子:158
专家分:447
注 册:2011-5-14
收藏
得分:0 
程序代码:
#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_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;

 } 


编译没有错,就是两警告!
2011-06-15 12:47
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
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
代码确实蛮长的

—>〉Sun〈<—
2011-06-16 23:04
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
// 该函数具体要做什么,请说明下。
NODE *merge(NODE *head1,NODE *head2);

—>〉Sun〈<—
2011-06-16 23:35
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
merge函数功能是将已知的head1,head2两个链表按num升序合并,若num相同则保留score较大的成员
2011-06-17 16:06
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:20 
楼主代码还是有很多问题的。

insert 的返回值需要返回来,赋值给head

insert(head, p);  该函数会重新设置头节点,函数并不能改变传递给他的值。虽然传递给它head,但不会改变该head变量中保存的地址。

Node* insert(NODE **ppHead, NODE*p);    // 函数原型
调用方法:
    NODE *head, *p;
    insert(&head, p);        // 这样,才能在被调函数中改变调用函数中的值。 只有这样函数才能改变上面那个 head 变量。

程序代码:
/***********
cosdos 
2011-06-17
***********/

#include <stdio.h>
#include <stdlib.h>
#include <string.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 *create(struct data_array * array, int n);
NODE* copy_list(NODE * head);
void display(NODE * head);
NODE * insert_merge(NODE * head, NODE * p0);
NODE *merge(NODE * a, NODE * b);
NODE *del(NODE * head1, NODE * head2);
void destory(NODE *p);
//-------------------------------------------------

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 = b = a_dup = b_dup = NULL;

    a = create(data_a, 5);
    b = create(data_b, 6);

    printf("a:\n");
    display(a);
    printf("b:\n");
    display(b);

    a_dup = copy_list(a);
    b_dup = copy_list(b);

    a = merge(a, b);
    printf("a:\n");
    display(a);

    b_dup = del(b_dup, a_dup);
    printf("b:\n");
    display(b_dup);

    destory(a_dup);
    destory(b_dup);
    destory(a);
    destory(b);
    
    //system("pause");
    return 0;
}


//-----------------------------------
// 修改了 create
NODE *create(struct data_array * array, int n)
{
    int i;
    NODE *p, *head;
    head = NULL;
    for (i = 0; i < n; i++) {
        p = (NODE *)malloc(sizeof(NODE));
        if (p == NULL)
            exit(1);
        p->num = array[i].num;
        p->score = array[i].score;
        p->next = NULL;

        head = insert(head, p); // 因为insert会改变head,所以把新的头节点赋值给head。
    }
    return head;
}

// 修改自楼主原来那个 insert
// 按num的升序插入节点,保留相同num的节点。
// 只插入,但不合并相同 num 节点。
//     因为不知道楼主是否需要在 insert 中就合并相同 num 的节点,
//     所以我另外写了一个,插入加合并的 insert_merge 函数。
NODE* insert(NODE *head, NODE *p0)
{
    NODE *p1, *p2;
    p2 = p1 = NULL;

    if (head == NULL)
        return p0;
    if (p0 == NULL)
        return head;

    p1 = head;
    while((p0->num>p1->num)&&(p1->next!=NULL)){
        p2=p1;
        p1=p1->next;
    }

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

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

    return head;
}

// 修改了 insert
// 插入合并
NODE * insert_merge(NODE * head, NODE * p0)
{
    NODE *p1, *p2;
    p1 = p2 = NULL;
    
    if (p0 == NULL)
        return head;
    if (head == NULL) {
        p0->next = NULL;
        return p0;
    }
    
    p1 = head;
    while ((p0->num > p1->num) && (p1->next != NULL)) {
        p2 = p1;
        p1 = p1->next;
    }
    
    // 一下是修改后的程序,在插入中也得合并相同num的节点。
    // 如果num相同,则把score大的留下
    if (p1 == head) {
        if (p0->num < p1->num) {
            p0->next = head;
            head = p0;
        } else if (p0->num == p1->num) {
            if (p0->score > p1->score) {
                p0->next = head->next;
                free (head);
                head = p0;
            } else {
                free(p0);
            }
        } else {
          // p0->num > p1->num
            head->next = p0;
            p0->next   = NULL;
        }
        return head;
    }
    
    if (p0->num == p1->num) {
        if (p0->score > p1->score) {
            p2->next = p0;
            p0->next = p1->next;
            free(p1);
        } else {
            free(p0);
        }
        return head;
    }

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



// 修改了 display
void display(NODE * head)
{
    if (head == NULL) {
        return;
    }
    printf("%-8s%s\n", "Num", "Score");
    do {
        printf("%-8ld%4d\n", head->num, head->score);
        head = head->next;
    } while (head != NULL);
}

// 修改了 copy_list
NODE* copy_list(NODE * head)
{
    NODE *l, *p, *front;
    int i;
    i = 0;
    front = l = p = NULL;

    while (head != NULL) {
        p = (NODE*)malloc(sizeof(NODE));
        if (p == NULL)
            exit(1);
        p->num = head->num;
        p->score = head->score;
        p->next = NULL;

        if (i == 0) {
            front = l = p;
        } else {
            front->next = p;
            front = p;
        }
        
        head = head->next;
        ++i;
    }
    return l;
}

// 修改了 merge
NODE *merge(NODE * a, NODE * b)
{
    NODE *next;
    if (a == NULL)
        return b;
    if (b == NULL)
        return a;

    // 如果在 create 中调用的是 insert_merge,下面3行可以省略。
    next = a->next;
    a->next = NULL;
    a = insert_merge(a, next);
    
    while (b != NULL) {
        next = b->next;
        a = insert_merge(a, b);
        b = next;
    }
    return a;
}

// 删除相同num的节点
NODE *del(NODE * head1, NODE * head2)
{
    NODE *p1 = head1, *p2 = head2, *temp;
    
    if (p2 == NULL)
        return p1;
    
    while (p2 != NULL) {
        while (p1 != NULL) {
            if (p1->num == p2->num) {
                if (p1 == head1)
                    head1 = p1->next;
                else
                    temp->next = p1->next;
                free(p1);
            }
            temp = p1;
            p1 = p1->next;
        }
        p2 = p2->next;
    }
    return head1;
}

void destory(NODE *p)
{
    NODE *n;
    while (p != NULL) {
        n = p->next;
        free(p);
        p = n;
    }
}


[ 本帖最后由 cosdos 于 2011-6-17 22:25 编辑 ]

—>〉Sun〈<—
2011-06-17 21:45
pqy330
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
回复 8楼 cosdos
太感谢了!真不好意思,没把软件目的说出来,还要你猜。这是我的疏忽。
2011-06-19 00:20
快速回复:以下代码有点长,望有耐心的高手帮忙找错
数据加载中...
 
   



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

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