求C=A∩B,A,B,C均为链式结构
#include <iostream>
using namespace std;
/*
*writer:yaojinhui
*/
using namespace std;
typedef struct LNode {
char data;
struct LNode* next;
}LNode, *Linklist;
Linklist tail_insert(Linklist &L) {//尾插法创建单链表
int n;
cout << "打算创建多少个节点?"<<endl;
cin >> n;
cout << "输入字符串,q结束输入" << endl;
L = (Linklist)malloc(sizeof(Linklist));
L->next = NULL;
LNode *p;
LNode *r = L;
char x;
while (true) {
cin >> x;
if (x == 'q') break;
p = (LNode*)malloc(sizeof(LNode));
p->data = x;
r->next = p;
r = p;
}
r->next = NULL;
return L;
}
int Search(Linklist L,char x)
{
LNode *p;
p=L->next;
while(p)
{
if(x==p->data)
{
// cout<<"查找成功!!"<<endl;
return 1;
}
p=p->next;
}
return 0;
}
int Insert(Linklist &L,char x)
{
LNode *q,*p;
p=L;//定义尾指针
q=(LNode *)malloc(sizeof(LNode));
q->data=x;
q->next=NULL;
while(p->next)
p=p->next;
p->next=q;
p=p;
return 1;
}
int ReOrder(Linklist L1,Linklist L2,Linklist &L3)
{
LNode *p;
p=L1->next;
while(p)
{
if(Search(L2,p->data))
{
Insert(L3,p->data);
// cout<<p->data<<endl;
}
p=p->next;
}
return 0;
}
void print(Linklist L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
void IninLinkList(Linklist &L)
{
L=(LNode *)malloc(sizeof(LNode));
L->next=NULL;
}
int main() {
Linklist L1,L2,L,L3;
IninLinkList(L3);
L1=tail_insert(L);
L2=tail_insert(L);
ReOrder(L1,L2,L3);
print(L1);
print(L2);
print(L3);
return 0;
}