C++单链表的交集和差集

需求
已知两个链表A和B分别表示两个集合,其元素按递增排列
1、求交集
2、求差集

1、基操

#include<iostream>
#define ok 1
#define error 0
typedef int Status;
typedef int ElemType;
using namespace std;

typedef struct LNode {
	ElemType data;
	struct LNode *next;
} LNode,*LinkList;

2、头插法

Status Create_List_Head(LinkList &L,int n) {
	LNode *p = NULL;
	L = new LNode;
	L->next = NULL;
	for(int i = 0; i < n; i++) {
		p = new LNode;
		cin >> p->data;
		p->next = L->next;
		L->next = p;
	}
	return ok;
}

3、尾插法

Status Create_List_Tail(LinkList &L,int n) {
	L = new LNode;
	L->next = NULL;
	LNode *r = NULL,*p = NULL;
	r = L;
	for(int i = 0; i < n; i++) {
		p = new LNode;
		cin >> p->data;
		r->next = p;
		p->next = NULL;
		r = p;
	}
	
	return ok;
}

4、打印链表

void PrintList(LinkList L) {
	LNode *p = NULL;
	p = L->next;
	while(p) {
		cout << p->data << " ";
		p = p->next;
	}
}

5、统计结点个数

int GetCount(LinkList L) {
	LNode *p = NULL;
	p = L->next;
	int cnt = 0;
	while(p) {
		cnt++;
		p = p->next;
	}
	
	return cnt;
}

6、交集

void PublicList(LinkList &A,LinkList &B) {
	LNode *p = A,*q = B->next,*tmp = NULL;
	LNode *w = p->next;
	while(w) {
		while(q && (w->data != q->data)) {
			q = q->next;
		}
		
		if(!q) {
			tmp = w;
			w = w->next;
			p->next = tmp->next;
			delete tmp;
		} else {	
			p = w;
			w = w->next;
		}
		q = B->next;
	}
}

7、差集

void DifferenceSet(LinkList &A,LinkList &B,LinkList &c) {
	LNode *p = A,*q = B->next,*w = A->next;
	LNode *head = new LNode;
	head->next = NULL; // 新的链表 
	LNode *t = head;
	while(w) {
		while(q && (w->data != q->data)) {
			q = q->next;
		}
		
		if(!q) {
			t->next = w;
			t = w;
			w = w->next;
			t->next = NULL;
		} else {
			p = w;
			w = w->next;
		}
		
		q = B->next;
	}
	
	c = head;
}

8、main里调用

int main() {
	LinkList A,B,C;
	Create_List_Tail(A,6);
	Create_List_Tail(B,3);
	PublicList(A,B);
	PrintList(A);
//	DifferenceSet(A,B,C);
//	PrintList(C);
//	cout << endl;
//	cout << "差集的个数是:" << GetCount(C) << endl; 
	return 0;
}

9、完整代码

#include<iostream>
#define ok 1
#define error 0
typedef int Status;
typedef int ElemType;
using namespace std;

typedef struct LNode {
	ElemType data;
	struct LNode *next;
} LNode,*LinkList;

Status Create_List_Head(LinkList &L,int n) {
	LNode *p = NULL;
	L = new LNode;
	L->next = NULL;
	for(int i = 0; i < n; i++) {
		p = new LNode;
		cin >> p->data;
		p->next = L->next;
		L->next = p;
	}
	return ok;
}

Status Create_List_Tail(LinkList &L,int n) {
	L = new LNode;
	L->next = NULL;
	LNode *r = NULL,*p = NULL;
	r = L;
	for(int i = 0; i < n; i++) {
		p = new LNode;
		cin >> p->data;
		r->next = p;
		p->next = NULL;
		r = p;
	}
	
	return ok;
}

void PrintList(LinkList L) {
	LNode *p = NULL;
	p = L->next;
	while(p) {
		cout << p->data << " ";
		p = p->next;
	}
}

int GetCount(LinkList L) {
	LNode *p = NULL;
	p = L->next;
	int cnt = 0;
	while(p) {
		cnt++;
		p = p->next;
	}
	
	return cnt;
}

void PublicList(LinkList &A,LinkList &B) {
	LNode *p = A,*q = B->next,*tmp = NULL;
	LNode *w = p->next;
	while(w) {
		while(q && (w->data != q->data)) {
			q = q->next;
		}
		
		if(!q) {
			tmp = w;
			w = w->next;
			p->next = tmp->next;
			delete tmp;
		} else {	
			p = w;
			w = w->next;
		}
		q = B->next;
	}
}

void DifferenceSet(LinkList &A,LinkList &B,LinkList &c) {
	LNode *p = A,*q = B->next,*w = A->next;
	LNode *head = new LNode;
	head->next = NULL; // 新的链表 
	LNode *t = head;
	while(w) {
		while(q && (w->data != q->data)) {
			q = q->next;
		}
		
		if(!q) {
			t->next = w;
			t = w;
			w = w->next;
			t->next = NULL;
		} else {
			p = w;
			w = w->next;
		}
		
		q = B->next;
	}
	
	c = head;
}



int main() {
	LinkList A,B,C;
	Create_List_Tail(A,6);
	Create_List_Tail(B,3);
	PublicList(A,B);
	PrintList(A);
//	DifferenceSet(A,B,C);
//	PrintList(C);
//	cout << endl;
//	cout << "差集的个数是:" << GetCount(C) << endl; 
	return 0;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值