【数据结构】【实验作业05】求两个集合的并集、交集、差集,集合用链表

返回临时对象会执行析构函数

我的解决方法时在函数内部用类指针,返回类指针就不会有问题了。
不知道大家还有没有其他解决方法。

复制构造函数和重载=的区别

这里不讲述为什么要使用复制构造函数和重载=(自行百度)

C++中调用复制构造函数的三种情况:

第一种:Myclass A=B;
第二种:fun(Myclass a); 对象作为参数传递
第三种:a = fun();
1.通过一个对象构造另一个对象
2.调用参数为对象的函数
3.调用返回值为对象的函数

题目:

在这里插入图片描述
注意:我自己写的myExceptions.h 就不放进来了。

chainNode.h:

#pragma once
#include<iostream>
template<class T>
struct chainNode
{
	T element;
	chainNode<T>* next;
	chainNode(){}
	chainNode(const T& element,chainNode<T>*next=NULL){
		this->element = element;
		this->next = next;
	}
	
};

chain.h:

#pragma once
#include"chainNode.h"
#include<sstream>
#include"myExceptions.h"
using namespace std;
template<class T>
class chain
{
public:
	chain();
	chain(const chain<T>&);
	~chain();

	bool empty()const;
	int size()const;
	T& get(int theIndex)const;
	void erase(int theIndex);
	void insert(int theIndex, const T& theElement);
	void append(const T& theElement);
	void output()const;
	chain<T>& operator=(const chain<T>& set);
	chain<T>*& union_set(const chain<T>& setB);//求并集
	chain<T>*& inter_set(const chain<T>& setB);//求交集
	chain<T>*& differ_set(const chain<T>& setB);//求差集
private:
	void checkIndex(int theIndex)const;
	chainNode<T>* firstNode;
	chainNode<T>* TailNode;
	int listSize;
};


chain.cpp:

#include "chain.h"
#include<algorithm>
template<class T>
chain<T>::chain()
{
	firstNode = NULL;
	TailNode = NULL;
	listSize = 0;
}
template<class T>
chain<T>::chain(const chain<T>&theList)
{
	listSize = theList.listSize;
	if (listSize == 0) {
		firstNode = NULL;
		return;
	}
	chainNode<T>* sourseNode = theList.firstNode;
	firstNode = new chainNode<T>(sourseNode->element);
	sourseNode = sourseNode->next;
	TailNode = firstNode;
	while (sourseNode!= NULL) {
		chainNode<T>* nextNode = new chainNode<T>(sourseNode->element);
		TailNode->next = nextNode;
		TailNode = nextNode;
		sourseNode = sourseNode->next;

	}
}
template<class T>
chain<T>::~chain()
{
	while (firstNode != NULL) {
		chainNode<T>* nextNode = firstNode->next;
		delete firstNode;
		firstNode = nextNode;
	}
}
template<class T>
bool chain<T>::empty() const
{
	return listSize == 0;
}
template<class T>
int chain<T>::size() const
{
	return listSize;
}
template<class T>
T& chain<T>::get(int theIndex) const
{
	checkIndex(theIndex);
	chainNode<T>* cur = firstNode;
	for (int i = 0; i < theIndex; i++) {
		cur = cur->next;
	}
	return cur->element;
}
template<class T>
void chain<T>::erase(int theIndex)
{
	checkIndex(theIndex);
	chainNode<T>* deleteNode;
	if (theIndex == 0) {
		deleteNode = firstNode;
		firstNode = firstNode->next;
		TailNode = NULL;
	}
	else {
		chainNode<T>* p = firstNode;
		for (int i = 0; i < theIndex - 1; i++) {
			p = p->next;
		}
		if (theIndex == listSize - 1) {
			TailNode = p;
		}
		deleteNode = p->next;
		p->next = deleteNode->next;

	}
	listSize--;
	delete deleteNode;
}
template<class T>
void chain<T>::insert(int theIndex, const T& theElement)
{
	if (theIndex < 0 || theIndex > listSize) {
		ostringstream s;
		s << "index = " << theIndex << " size = " << listSize;
		throw illegalIndex(s.str());
	}
	if (theIndex == 0) {
		firstNode = new chainNode<T>(theElement,firstNode);
		TailNode = firstNode;
	}
	else{
		chainNode<T>* p = firstNode;
		for (int i = 0; i < theIndex - 1; i++) {
			p = p->next;
		}
		chainNode<T>* temp = new chainNode<T>(theElement);
		temp->next = p->next;
		p->next = temp;
		if (theIndex == listSize) {
			TailNode = temp;
		}
	}
	
	listSize++;
}

template<class T>
void chain<T>::append(const T& theElement)
{
	if (listSize == 0) {
		firstNode = new chainNode<T>(theElement, firstNode);
		TailNode = firstNode;
	}
	else {
		chainNode<T>* temp = new chainNode<T>(theElement);
		TailNode->next = temp;
		TailNode = temp;
	}
	listSize++;
}

template<class T>
void chain<T>::output() const
{
	chainNode<T>* cur = firstNode;
	while (cur != NULL) {
		cout << cur->element << " ";
		cur = cur->next;
	}
	cout << "\n";
}

template<class T>
chain<T>& chain<T>::operator=(const chain<T>& set)
{	
	while (firstNode != NULL) {
		chainNode<T>* nextNode = firstNode->next;
		delete firstNode;
		firstNode = nextNode;
	}
	listSize = set.listSize;
	if (listSize == 0) {
		firstNode = NULL;
		return *this;
	}
	chainNode<T>* sourseNode = set.firstNode;
	firstNode = new chainNode<T>(sourseNode->element);
	sourseNode = sourseNode->next;
	TailNode = firstNode;
	while (sourseNode != NULL) {
		chainNode<T>* nextNode = new chainNode<T>(sourseNode->element);
		TailNode->next = nextNode;
		TailNode = nextNode;
		sourseNode = sourseNode->next;

	}
	return *this;
}





template<class T>
chain<T>*& chain<T>::union_set(const chain<T>& setB)
{

	T* A = new T[listSize];
	T* B = new T[setB.listSize];
	chainNode<T>* cur1 = firstNode;
	chainNode<T>* cur2 = setB.firstNode;
	for (int i = 0; i < listSize; i++) {
		A[i] = cur1->element;
		cur1 = cur1->next;
	}
	for (int i = 0; i < setB.listSize; i++) {
		B[i] = cur2->element;
		cur2 = cur2->next;
	}
	sort(A, A + listSize);
	sort(B, B + setB.listSize);
	int p = 0, q = 0;
	chain<T>* c = new chain<T>();
	while (p < listSize && q < setB.listSize) {
		if (A[p] <= B[q]) {
			c->append(A[p]);
			if (A[p] == B[q]) {
				q++;
			}
			p++;
		}
		else {
			c->append(B[q]);
			q++;
		}
	}
	while (p < listSize) {
		c->append(A[p]);
		p++;
	}
	while (q < setB.listSize) {
		c->append(B[q]);
		q++;
	}
	return c;

}

template<class T>
chain<T>*& chain<T>::inter_set(const chain<T>& setB)
{
	T* A = new T[listSize];
	T* B = new T[setB.listSize];
	chainNode<T>* cur1 = firstNode;
	chainNode<T>* cur2 = setB.firstNode;
	for (int i = 0; i < listSize; i++) {
		A[i] = cur1->element;
		cur1 = cur1->next;
	}
	for (int i = 0; i < setB.listSize; i++) {
		B[i] = cur2->element;
		cur2 = cur2->next;
	}
	sort(A, A + listSize);
	sort(B, B + setB.listSize);
	int p = 0, q = 0;
	chain<T>* c = new chain<T>();
	while (p < listSize && q < setB.listSize) {
		if (A[p] < B[q]) {
			p++;
		}
		else if (A[p] > B[q]) {
			q++;
		}
		else {
			c->append(A[p]);
			p++;
			q++;
		}
	}
	return c;
}

template<class T>
chain<T>*& chain<T>::differ_set(const chain<T>& setB)
{
	T* A = new T[listSize];
	T* B = new T[setB.listSize];
	chainNode<T>* cur1 = firstNode;
	chainNode<T>* cur2 = setB.firstNode;
	for (int i = 0; i < listSize; i++) {
		A[i] = cur1->element;
		cur1 = cur1->next;
	}
	for (int i = 0; i < setB.listSize; i++) {
		B[i] = cur2->element;
		cur2 = cur2->next;
	}
	sort(A, A + listSize);
	sort(B, B + setB.listSize);
	int p = 0, q = 0;
	chain<T>* c = new chain<T>();
	while (p < listSize && q < setB.listSize) {
		if (A[p] == B[q]) {
			p++;
			q++;
		}
		else {
			c->append(A[p]);
			p++;
		}
	}
	return c;
}

template<class T>
void chain<T>::checkIndex(int theIndex) const
{
	if (theIndex < 0 || theIndex >= listSize)
	{
		ostringstream s;
		s << "index = " << theIndex << " size = " << listSize;
		throw illegalIndex(s.str());
	}

}


test.cpp:

#include<iostream>
using namespace std;
#include"chain.h"
#include"chain.cpp"
int main() {
	chain<int> a;
	chain<int> b;
	a.append(2);
	a.append(4);
	a.append(1);
	a.append(3);
	a.append(5);
	b.append(2);
	b.append(5);
	b.append(10);
	cout << "集合A中的元素有:";
	a.output();
	cout << "集合B中的元素有:";
	b.output();
	chain<int> *c = a.union_set(b);
	cout << "集合A和集合B的并集中的元素有:";
	c->output();
	cout << "集合A和集合B的交集中的元素有:";
	c = a.inter_set(b);
	c->output();
	cout << "集合A和集合B的差集中的元素有:";
	c = a.differ_set(b);
	c->output();

	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值