求两个单链表的差集,并以递增形式存储。(C++)

(1)实现节点类模板,放在node.h文件中;
(2)实现线性链表类模板,放在lk_list.h文件中;
(3)实现lc返回la与lb表示集合的差集,并使lc中数据元素仍递增有序,放在alg.h文件中。

main.cpp

#include <iostream>						// 标准流操作
using namespace std;					// 标准库包含在命名空间std中
#include "lk_list.h"				// 线性链表
#include "alg.h"					// 算法
#include "ass.h"					// 辅助函数


int main()							// 主函数main()
{
	int a[] = { 1, 3, 7, 9, 12 }, n = 5;
	int b[] = { 2, 3, 8, 9, 13 }, m = 5;
	LinkList<int> la, lb, lc;
	Create<int>(la, a, n);			// 构造线性表la	
	Create<int>(lb, b, m);			// 构造线性表lb
	Difference<int>(la, lb, lc);	// 求差集
	cout << "la:";
	Show<int>(la);				// 显示la
	cout << endl;					// 换行
	cout << "lb:";
	Show<int>(lb);				// 显示lb
	cout << endl;					// 换行
	cout << "lc:";
	Show<int>(lc);				// 显示lc
	cout << endl;					// 换行
	system("pause");
	return 0;					// 返回值0, 返回操作系统
}

node.h

#pragma once
template<class ElemType>
struct Node
{
	//数据成员
	ElemType data;//数据成分
	Node<ElemType>*next;//指针成分

	//构造函数模板
	Node();//无参数构造函数模板
	Node(const ElemType &e, Node<ElemType>*link = NULL);//已知数据元素值和指针建立节点
};

//节点类模板的实现部分
template<class ElemType>
Node<ElemType>::Node()
//构造指针成分为空的节点
{
	next = NULL;
}

template<class ElemType>
Node<ElemType>::Node(const ElemType &e, Node<ElemType>*link)
//构造一个数据成分为e和指针成分为link的节点
{
	data = e;
	next = link;
}

lk_list.h

#pragma once
#include"node.h"

template<class ElemType>
class LinkList
{
public:
	//数据成员
	Node<ElemType>*head;
	//辅助函数模板
	Node<ElemType>*GetElemPtr(int position)const;

public:
	
	LinkList();
	virtual ~LinkList();
	int Length()const;
	bool Insert(int position, const ElemType&e);
	bool GetElem(int position, ElemType &e) const;
	void Clear();
	bool Empty()const;
	bool Delete(int position);
};

template <class ElemType>
Node<ElemType>*LinkList<ElemType>::GetElemPtr(int position)const
{
	Node<ElemType>*temPtr = head;
	int temPos = 0;

	while (temPtr != NULL&&temPos < position)
	{
		temPtr = temPtr->next;
		temPos++;
	}

	if (temPtr != NULL&&temPos == position)
	{
		return temPtr;
	}
	else
	{
		return NULL;
	}
}

template <class ElemType>
bool LinkList<ElemType>::Delete(int position)
{
	if (position<1 || position>Length())
	{
		return false;
	}
	else
	{
		Node<ElemType>* temPtr = GetElemPtr(position - 1);
		Node<ElemType>* nextPtr = temPtr->next;
		temPtr->next = nextPtr->next;
		delete nextPtr;
		return true;
	}
}

template <class ElemType>
LinkList<ElemType>::LinkList()
{
	head = new Node<ElemType>;
}

template <class ElemType>
LinkList<ElemType>::~LinkList()
{
	Clear();
	delete head;
}

template <class ElemType>
int LinkList<ElemType>:: Length()const
{
	int count = 0;
	for (Node<ElemType>*temPtr = head->next; temPtr != NULL; temPtr = temPtr->next)
	{
		count++;
	}
	return count;
}

template <class ElemType>
bool LinkList<ElemType>::Insert(int position, const ElemType&e)
{
	if (position<1 || position>Length() + 1)
	{
		return false;
	}
	else
	{
		Node<ElemType>*temPtr = GetElemPtr(position - 1);

		Node<ElemType>*newPtr = new Node<ElemType>(e, temPtr->next);
		
		temPtr->next = newPtr;
		
		return true;
	}
}

template <class ElemType>
bool LinkList<ElemType>::GetElem(int position, ElemType &e)const
{
	if (position<1 || position>Length())
	{
		return false;
	}
	else
	{
		Node<ElemType>*temPtr = GetElemPtr(position);
		e = temPtr->data;
		return true;
	}
}

template <class ElemType>
bool LinkList<ElemType>::Empty()const
{
	return head->next == NULL;
}


template <class ElemType>
void LinkList<ElemType>::Clear()
{
	
	while (!Empty())
	{
		Delete(1);
	}
}

alg.h

#pragma once
template <class ElemType>
void Difference(const LinkList<ElemType> &la, const LinkList<ElemType> &lb, LinkList<ElemType> &lc)
{
	Node<ElemType>*a = la.GetElemPtr(1);
	Node<ElemType>*b = lb.GetElemPtr(1);
	int c = 1;
	for (int i = 1; i <= la.Length(); i++)
	{
		if (a->data == b->data)
		{
			a = a->next;
			b = b->next;
		}
		else
		{
			if (a->data > b->data)
			{
				lc.Insert(c, b->data);
				c++;
				lc.Insert(c, a->data);
				c++;
				a = a->next;
				b = b->next;
			}
			else
			{
				lc.Insert(c, a->data);
				c++;
				lc.Insert(c, b->data);
				c++;
				a = a->next;
				b = b->next;
			}
		}
	}
}

ass.h

#pragma once
// 辅助函数 ass.h, 根据数组构建单链表,显示单链表。

template <class ElemType>
void Create(LinkList<ElemType> &la, ElemType a[], int n)
// 操作结果:由数组a存储的n个元素构造线性表
{
	la.Clear();				// 清空la
	for (int position = 1; position <= n; position++)
		la.Insert(position, a[position - 1]);	//向la插入元素
}

template <class ElemType>
void Show(LinkList<ElemType> &la)
// 操作结果:显示线性表la
{
	int e;					// 元素	
	for (int position = 1; position <= la.Length(); position++)
	{	// 依次取出并显示各元素
		la.GetElem(position, e);				// 取出元素
		cout << e << " ";						// 显示元素
	}
}

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GT-一二

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

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

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

打赏作者

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

抵扣说明:

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

余额充值