已知无头单链表A和B表示两个集合,用算法实现A=A-B补集

已知无头单链表A和B表示两个集合,用算法实现A=A-B补集
数据结构
jianghuiyili 1年前 已收到1个回答 举报

69128431 幼苗

共回答了11个问题采纳率:90.9% 举报

data_int
#include "head.h"
struct LNode{
// char data[10];
int data;
struct LNode *next;
};
typedef struct LNode * LinkList;
void InitList_L(LinkList &L)//链表构造函数
{
L=new LNode;
L->next=NULL;
}
void PrintList_L(LinkList &H)//链表显示函数
{
LinkList L=H;
L=L->next;
while(1)
{
cout<<"data value is "<data< L=L->next;
if (L==NULL)
return;
}
}
void Insert_L(LinkList &H,int n=0)//插入链表
{
LinkList L=H;
LinkList p=L;
int i=0;
if (n==0)
{
n=1;
while(p->next!=NULL)
{
p=p->next;
n++;
}

}
else if (n<1)
{
cout<<"error"< return;
}
for (i=0;i {
if (L->next==NULL)
{
cout<<"error"< return;
}
L=L->next;
}
p=new LNode;
cout<<"please input a value:";
cin>>p->data;
p->next=L->next;
L->next=p;
}
LinkList bing_LinkList(LinkList a,LinkList b)
{
LinkList c;
LinkList nc;
LinkList t;
InitList_L(c);
nc=c;
a=a->next;
while (a!=NULL)//复制a到c
{
t=new LNode;
t->data=a->data;
nc->next=t;
t->next=NULL;
nc=nc->next;
a=a->next;
}
b=b->next;
while (b!=NULL)
{
nc=c;
while (nc->next!=NULL)
{
if (nc->next->data==b->data)
break;
nc=nc->next;
}
if (nc->next==NULL)
{
t=new LNode;
t->data=b->data;
nc->next=t;
t->next=NULL;
nc=nc->next;
}
b=b->next;
}
return c;
}
void main()
{
LinkList a,b,c;
int i=0;

InitList_L(a);
cout<<"nI will input date."< for (i=1;i<=3;i++)
Insert_L(a,i);
// PrintList_L(a);
InitList_L(b);
cout<<"nI will input date."< for (i=1;i<=3;i++)
Insert_L(b,i);
// PrintList_L(b);
c=bing_LinkList(a,b);
PrintList_L(c);

}

1年前

9
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.015 s. - webmaster@yulucn.com