求C=A∩B,A,B,C均为链式结构

求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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

折戟不必沉沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值