kangdi 发表于 2008-3-28 20:10

帮忙看看那有错误:把链表A,B,C中都有的元素放入A中

//把链表A,B,C中都有的元素放入A中

#include<iostream.h>
#include<malloc.h>
#include<iomanip.h>
#define NuLL 0
#define Len sizeof( Lnode)
typedef struct Lnode{
    float  data;
    struct Lnode * next;
  }Lnode ,*LinkList;
void Creatlist_L(LinkList &L,int n)
{
        LinkList r,p;
        int i;
        L=(LinkList)malloc(Len);
        L->next=NuLL;
        r=L;
        for(i=0;i<n;i++)
        {
       p=(LinkList)malloc(Len);
           cin>>p->data;
           p->next=NuLL;
           r->next=p;
           r=p;
        }
}

void Print_L(LinkList L)
{
        LinkList p,q;
        p=L;
        q=p->next;
        if(q==NuLL)
        cout<<"There is nothing in LinkList L";
        while(q!=NuLL)
        {
                cout<<q->data<<setw(3);
                q=q->next;
        }
}

void Pub(LinkList &La,LinkList Lb,LinkList Lc)
{
        LinkList q,r,pa,pb,pc,s;
        pa=La->next;


                while(pa!=NuLL&&pa->next!=NuLL)
                {
                        q=pa->next;
                        if(pa->data==q->data)
                        {pa->next=q->next;
                         free(q);
                        }
                       
                        pa=pa->next;
                }//删除LA中相同的元素


                pa=La->next;
                r=La;
                //pb=Lb->next;
                //pc=Lc->next;
       
     while(pa!=NuLL)
         {    pb=Lb->next;
                  pc=Lc->next;
                 while(pb!=NuLL&&pb->data!=pa->data)
                   pb=pb->next;
                 while(pc!=NuLL&&pc->data!=pa->data)
                   pc=pc->next;
                if(pa->data==pb->data&&pa->data==pc->data)
               
                {r=pa;
                 pa=pa->next;//pb=Lb->next;
                 //pc=Lc->next;
                }
                else
                {   
                 s=pa;
                 r->next=pa->next;
                 pa=r->next;
                 free(s);
                // pb=Lb->next;
                 //pc=Lc->next;
                }
               
         }
}

void main()
{
        LinkList A,B,C,K;
        int n,m,j;
        cout<<"pleast input n=";
        cin>>n;
    Creatlist_L(A,n);

    cout<<"pleast input m=";
        cin>>m;
    Creatlist_L(B,m);

        cout<<"pleast input j=";
        cin>>j;
    Creatlist_L(C,j);

        cout<<"The ListList A are:"<<endl;
    Pub(A,B,C);
    Print_L(A);
   
}


页: [1]

编程论坛