编程论坛's Archiver

hhspq2007 发表于 2007-12-14 23:10

让我想死的代码,帮忙看下

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define NULL 0
#define LEN sizeof(node)
typedef int elemtype;
struct node
{
        elemtype data;
        struct node*next;
};
struct node*Createlist()
{
        node*head=NULL,*p=NULL,*r=NULL;
        head=(node*)malloc(LEN);
        r=head;
        cout<<"请输入链表的长度:"<<endl;
        int n,temp;
        cin>>n;
        cout<<"请按顺序输入链表元素:"<<endl;
        for(int i=0;i<n;i++)
        {
                p=(node*)malloc(LEN);
                cin>>temp;
                p->data=temp;
                r->next=p->next;
                r=p;
        }
        return (head);
}
void Traverser(struct node*head)
{
        if(head->next==NULL)
        {
                cout<<"Error!"<<endl;
                exit(1);
        }
        else
        {
                node*r=head->next;
                while(r!=NULL)
                {
                        cout<<r->data<<" ";
                        r=r->next;
                }
        }
}
int IsEmpty(struct node*head)
{
        if(head->next==NULL)
                return 1;
        else
                return 0;
}
void Mergelist(node*La,node*Lb,node*Lc)
{
        node*pa=NULL,*pb=NULL,*pc=NULL;
        Lc=pc=La;
        while(pa&&pb)
        {
                if(pa->data<=pb->data)
                {
                        pc->next=pa;
                        pc=pa;
                        pa=pa->next;
                }
                else
                {
                        pc->next=pb;
                        pc=pb;
                        pb=pb->next;
                }
        }
                pc->next=pa?pa:pb;
                free(Lb);
}
void main()
{
        struct node*La=NULL,*Lb=NULL,*Lc=NULL,*p;
        La=Createlist();
        Lb=Createlist();
        Mergelist(La,Lb,Lc);
        p=Lc->next;
        if(p==NULL)
        {
                cout<<"biao wei kong"<<endl;
        }
        else
        {
                while(p!=NULL)
                {
                        cout<<p->data<<" ";
                        p=p->next;
                }
        }
       
}
看看吧,我很无奈了,初学者,大家鼓励下,谢谢

hhspq2007 发表于 2007-12-14 23:17

最后那个循环是怀疑调用Createlist失败加上的

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.