标题:用链表存一组数,程序挂了,求助
只看楼主
南国雨
Rank: 1
等 级:新手上路
帖 子:26
专家分:9
注 册:2015-2-1
结帖率:72.73%
 问题点数:0 回复次数:2 
用链表存一组数,程序挂了,求助
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define ARRAY1_LENGTH 10
#define ARRAY2_LENGTH 8
typedef struct data{
    int number;
    struct data *next;
}*Pnumber,numbdata;
Pnumber p,pTop,pTemp;
void List_Init(Pnumber p){
    p=(Pnumber)malloc(sizeof(numbdata));
    if(!p){
        printf("Error");
        } 
}
void Select(int a[],int* b,int i){
    *b=a[i];    
}
int Compare(int a1,int a2){
    if(a1<a2){
        return 1;
    }
    else{
        return 0;
    }
}
void Insert(int *p,int b){
    *p=b;
}
int main(){
    int a[ARRAY1_LENGTH],b[ARRAY2_LENGTH];
    int i,j,k,elem1,elem2,d;
    printf("请向数组a输入10个非递减数字:\n"); 
    for(i=0;i<ARRAY1_LENGTH;i++){
        scanf("%d",&a[i]); 
    }
    printf("请向数组b输入8个非递减数字:\n");
    for(i=0;i<ARRAY2_LENGTH;i++){
        scanf("%d",&b[i]);
    } 
    i=0,j=0,k=0;
    List_Init(p);
    pTemp=p;
    pTop=p;
    while(i<ARRAY1_LENGTH-1&&j<ARRAY2_LENGTH-1){
        Select(a,&elem1,i);
        Select(b,&elem2,j); 
        d=Compare(elem1,elem2);
        if(d){
            Insert(&pTop->number,elem1);
            List_Init(p);
            p->next=p;
            pTop=pTop->next;
            i++;
        }
        else{
            Insert(&pTop->number,elem2);
            List_Init(p);
            p->next=p;
            pTop=pTop->next;
            j++;
        }
    }
    while(i<ARRAY1_LENGTH){
        Select(a,&elem1,i);
        Insert(&pTop->number,elem1);
        List_Init(p);
        p->next=p;
        pTop=pTop->next;
        i++;
    }
    while(j<ARRAY2_LENGTH){
        Select(b,&elem2,j);
        Insert(&pTop->number,elem2);
        List_Init(p);
        p->next=p;
        pTop=pTop->next;
        j++;
    }
    pTop=pTemp;
    for(i=0;i<ARRAY1_LENGTH+ARRAY2_LENGTH;i++){
        printf("%d\t",pTop->number);
        pTop=pTop->next;
    }
    return 0;
}
    

    
    
        


    
    
     


各位,可以跑起来,但是一跑就程序停止运行了。
各位看看是哪里出现了问题。
搜索更多相关主题的帖子: number 
2015-03-22 12:46
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
得分:0 
试着写了一下,测试了几次感觉不完善

楼主的意图是不是由两个升序数组合成一个降序链表?

[ 本帖最后由 longwu9t 于 2015-3-23 19:44 编辑 ]
收到的鲜花
  • 南国雨2015-04-06 15:30 送鲜花  1朵   附言:我很赞同

Only the Code Tells the Truth             K.I.S.S
2015-03-23 19:39
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>

#define LEN1 5
#define LEN2 3
#define SIZEND sizeof(ND)

#define MALLOC(p)\
    if(!(p = malloc(SIZEND))) {\
        fprintf(stderr, "malloc error...");\
        exit(EXIT_FAILURE);\
    }

#define FREE(p) if(p) free(p), p = NULL

typedef struct ND {
    int n;
    struct ND *next;
} ND, *PN;

int cmp(int a, int b) {
    return a < b ? 1 : 0;
}

void freeList(PN top) {
    PN p;

    while(top != NULL) {
        p = top->next;
        free(top);
        top = p;
    }
}

int main(void) {
    PN cur = NULL, top = NULL;
    int a[LEN1], b[LEN2], i, j;

    printf("请向数组a输入%d个升序数字:\n", LEN1);

    for(i = 0; i < LEN1; i++)
        scanf("%d", a + i);

    printf("请向数组b输入%d个升序数字:\n", LEN2);

    for(j = 0; j < LEN2; j++)
        scanf("%d", b + j);

    MALLOC(top);
    top->next = NULL;

    for(i = 0, j = 0; i < LEN1 || j < LEN2;) {
        if((cmp(a[i], b[j]) > 0)) {
            if(i < LEN1) top->n = a[i++];

            else top->n = b[j++];

        } else if(j < LEN2) {
            top->n = b[j++];

        } else top->n = a[i++];

        MALLOC(cur);
        cur->next = top;
        top = cur;
    }

    cur = top;
    top = top->next;
    FREE(cur);

    for(cur = top; cur != NULL; cur = cur->next)
        printf("%d  ", cur->n);

    puts("");

    freeList(top);

    return 0;
}

Only the Code Tells the Truth             K.I.S.S
2015-03-23 20:47



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




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

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