C++(数据结构与算法):28---字典的实现(链表形式)

一、前言

二、代码实现

  • 我们的字典采用键值升序的方式来对字典进行排序。插入字典时,如果键已经存在,则更新值;如果键不存在,则插入键

头文件

#include <iostream>
#include  <string>
#include <utility>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::pair;

链表节点定义

template<class K,class E>
struct pairNode
{
	typedef pair<const K, E> pairType;
	pairType element;
	pairNode<K, E>* next;
	pairNode(const pairType& thePair):element(thePair){}
	pairNode(const pairType& thePair, pairNode<K, E>* theNext) :element(thePair)
	{
		next = theNext;
	}
};

字典抽象类定义

template<class K,class E>
class dictionary
{
public:
	virtual ~dictionary() = default;
	virtual bool empty()const = 0; //判断字典是否为空
	virtual int size()const = 0; //返回字典中数对的数目
	virtual std::pair<const K, E>* find(const K&)const = 0; //返回匹配数对的指针
	virtual void erase(const K&) = 0; //删除匹配的数对
	virtual void insert(const std::pair<const K, E>&) = 0; //往字典中插入一个数对
};

字典类定义

template<class K,class E>
class sortedChain :public dictionary<K, E>
{
public:

	sortedChain();
	~sortedChain();
	bool empty()const override;
	int size()const override;
	std::pair<const K, E>* find(const K& theKey)const override;
	void erase(const K& theKey) override;
	void insert(const std::pair<const K, E>& thePair) override;
private:
	pairNode<K, E>* firstNode;
	int dSize;
};

template<class K, class E>
sortedChain<K, E>::sortedChain()
{
	this->firstNode = nullptr;
	this->dSize = 0;
}

template<class K, class E>
sortedChain<K, E>::~sortedChain()
{
	pairNode<K, E>* tempNode;
	while (this->firstNode) {
		tempNode = this->firstNode->next;
		delete this->firstNode;
		this->firstNode = tempNode;
	}
}

template<class K, class E>
bool sortedChain<K, E>::empty()const
{
	return this->dSize == 0;
}

template<class K, class E>
int sortedChain<K, E>::size()const
{
	return this->dSize;
}

template<class K, class E>
std::pair<const K, E>* sortedChain<K, E>::find(const K& theKey)const
{
	pairNode<K, E>* tempNode = this->firstNode;

	//遍历字典
	while ((tempNode != nullptr) && (tempNode->element.first != theKey))
		tempNode = tempNode->next;

	//如果找到了就返回
	if ((tempNode != nullptr) && (tempNode->element.first == theKey))
		return &tempNode->element;

	//没有找到就返回空
	return nullptr;
}

template<class K, class E>
void sortedChain<K, E>::erase(const K& theKey)
{
	pairNode<K, E>* eraseNode = this->firstNode;
	pairNode<K, E>* beforeEraseNode = nullptr;

	//遍历字典
	while ((eraseNode != nullptr) && (eraseNode->element.first < theKey))
	{
		beforeEraseNode = eraseNode;
		eraseNode = eraseNode->next;
	}

	//找到了就删除
	if ((eraseNode != nullptr) && (eraseNode->element.first == theKey))
	{
		//如果删除的是头结点
		if (eraseNode == this->firstNode){
			this->firstNode = eraseNode->next;
		}
		else{
			beforeEraseNode->next = eraseNode->next;
		}
		delete eraseNode;
		this->dSize--;
	}
}

template<class K, class E>
void sortedChain<K, E>::insert(const std::pair<const K, E>& thePair)
{
	pairNode<K, E>* afterInsertNode = this->firstNode;
	pairNode<K, E>* beforeInsertNode = nullptr;

	//遍历字典
	while ((afterInsertNode != nullptr) && (afterInsertNode->element.first < thePair.first))
	{
		beforeInsertNode = afterInsertNode;
		afterInsertNode = afterInsertNode->next;
	}

	//如果键已经存在,更新值
	if ((afterInsertNode != nullptr) && (afterInsertNode->element.first == thePair.first))
	{
		afterInsertNode->element.second = thePair.second;
		return;
	}

	//如果键不存在,插入新节点
	pairNode<K, E>* insertNode = new pairNode<K,E>(thePair, afterInsertNode);
	if (beforeInsertNode == nullptr)
		this->firstNode = insertNode;
	else
		beforeInsertNode->next = insertNode;
	this->dSize++;
}

主函数

int main()
{
	sortedChain<int, char> *mySortedChain = new sortedChain<int, char>();
	
	mySortedChain->insert(std::pair<int, char>(1,'a'));
	mySortedChain->insert(std::pair<int, char>(2, 'b'));
	mySortedChain->insert(std::pair<int, char>(3, 'c'));

	std::cout << "Current size " << mySortedChain->size() << std::endl;

	mySortedChain->erase(1);
	std::cout << "Current size " << mySortedChain->size() << std::endl;

	std::pair<const int, char> *findPair;
	findPair = mySortedChain->find(2);
	std::cout << "Find success,key is " << findPair->first << ",value is " << findPair->second << std::endl;


	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

董哥的黑板报

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

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

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

打赏作者

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

抵扣说明:

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

余额充值