当前位置: 代码迷 >> C语言 >> 帮看下这代码
  详细解决方案

帮看下这代码

热度:275   发布时间:2007-12-14 22:55:45.0
帮看下这代码
#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;
        }
    }
    
}
哪一位大虾帮我看下,为什么就是不能实行!!在下在这谢谢各位啦!!急用~~
搜索更多相关的解决方案: node  NULL  int  temp  链表  

----------------解决方案--------------------------------------------------------
为什么就是不能实行
你的里面用到C的标准库,又用到C++的流文件,建议你用其中的一种,如果用C,那么要用到#include<malloc.h>文件.如果用C++,最好用new来申请内存单元
----------------解决方案--------------------------------------------------------
????????????????????/
----------------解决方案--------------------------------------------------------
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#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;
        p->next=NULL;
        r->next=p;
        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=La->next,*pb=Lb->next,*pc=NULL;
    (*Lc)=(node*)malloc(LEN);
    (*Lc)->next=NULL;
    pc=*Lc;
    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;
}
void main()
{
    struct node*La=NULL,*Lb=NULL,*Lc=NULL,*p=NULL;
    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;
        }
    }

}

你的代码其实并不能排序,因为要想实现排序的功能,在创建链表的时候,就应该从小到大输入进去,不过现在没有内存错误了
----------------解决方案--------------------------------------------------------
真可以了,谢谢楼上的,呵呵
----------------解决方案--------------------------------------------------------
  相关解决方案