数据结构实现 5.2:映射_基于链表实现(C++版)

1. 概念及基本框架

.
5.1 中我们通过 二分搜索树 来实现了映射,这一节我们通过 链表 来实现映射。

映射
映射 的基本特性依然要满足:
1.映射内元素包含 键(key)值(value) ,而且一一对应。
2.映射内的元素的键 不能重复
注:有些映射(多重映射)中元素的键也可以重复。
2.1 中实现的链表结点只有一个数据,所以我们需要从底层进行一些改进去适应映射这一数据结构。首先来定义链表的结点类:

template <class K, class V>
class MapNode{
public:
	MapNode(K key = NULL, V value = NULL, MapNode<K, V> *next = NULL){
		m_key = key;
		m_value = value;
		this->next = next;
	}
public:
	K m_key;
	V m_value;
	MapNode<K, V> *next;
};

这个结点类的内部也显式的给出了构造函数,下面通过结点类来创建一个映射类。
注:这里我们也可以先实现一个链表类,然后通过链表来实现映射类。
有了改进版的链表结点,我们就可以利用一个由 纯虚函数 构成的 抽象类 作为一个接口来定义这些操作。具体代码如下:

template <class K, class V>
class Map{
public:
	virtual int size() = 0;
	virtual bool isEmpty() = 0;
	//增加操作
	virtual void add(K key, V value) = 0;
	//删除操作
	virtual V remove(K key) = 0;
	//修改操作
	virtual void set(K key, V value) = 0;
	//查找操作
	virtual bool contains(K key) = 0;
	virtual V get(K key) = 0;
};

下面只需要通过继承 抽象类,并且重写 纯虚函数 ,就可以完成 映射 的实现。映射类的框架如下:

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	LinkedListMap(){
		head.m_key = NULL;
		head.m_value = NULL;
		head.next = NULL;
		m_size = 0;
	}
	...
private:
	MapNode<K, V> head;
	int m_size;
};

这里为了避免重复设计就可以兼容更多数据类型,引入了 泛型 ,即 模板 的概念。(模板的关键字是 classtypename
这里链表加了虚拟头结点,同样,为了保护数据,变量设置为 private
实现了前面的程序之后,接下来就是一个映射的增、删、改、查以及一些其他基本操作,接下来利用代码去实现。

2. 基本操作程序实现

2.1 增加操作

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	...
	//增加操作
	void add(K key, V value){
		if (!contains(key)){
			MapNode<K, V> *p = new MapNode<K, V>(key, value, head.next);
			head.next = p;
			m_size++;
		}
	}
	...
};

需要查询链表是不是已经包含该键。

2.2 删除操作

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	...
	//删除操作
	V remove(K key){
		MapNode<K, V> *pre = &head;
		MapNode<K, V> *node = pre->next;
		while (node){
			if (key == node->m_key){
				pre->next = node->next;
				V res = node->m_value;
				delete node;
				m_size--;
				return res;
			}
			pre = node;
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
		return NULL;
	}
	...
};

2.3 修改操作

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	...
	//修改操作
	void set(K key, V value){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				node->m_value = value;
				return;
			}
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
	}
	...
};

2.4 查找操作

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	...
	//查找操作
	bool contains(K key){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				return true;
			}
			node = node->next;
		}
		return false;
	}
	V get(K key){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				return node->m_value;
			}
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
		return NULL;
	}
	...
};

2.5 其他操作

映射还有一些其他的操作,包括 映射大小 的查询等操作。

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	...
	int size(){
		return m_size;
	}
	bool isEmpty(){
		return m_size == 0;
	}
	...
};

3. 算法复杂度分析

3.1 增加操作

函数 最坏复杂度 平均复杂度
add O(n) O(n/2) = O(n)

因为需要判断是否存在该元素,所以需要遍历一遍整个链表。

3.2 删除操作

函数 最坏复杂度 平均复杂度
remove O(n) O(n/2) = O(n)

3.3 修改操作

函数 最坏复杂度 平均复杂度
set O(n) O(n/2) = O(n)

3.4 查找操作

函数 最坏复杂度 平均复杂度
contains O(n) O(n/2) = O(n)
get O(n) O(n/2) = O(n)

总体情况:

操作 时间复杂度
O(n)
O(n)
O(n)
O(n)

与利用二分搜索树实现的映射相比,利用链表实现的映射操作的时间复杂度就增大了很多。

4. 完整代码

程序完整代码(这里使用了头文件的形式来实现类)如下。

抽象类 接口代码:

#ifndef __MAP_H__
#define __MAP_H__

template <class K, class V>
class Map{
public:
	virtual int size() = 0;
	virtual bool isEmpty() = 0;
	//增加操作
	virtual void add(K key, V value) = 0;
	//删除操作
	virtual V remove(K key) = 0;
	//修改操作
	virtual void set(K key, V value) = 0;
	//查找操作
	virtual bool contains(K key) = 0;
	virtual V get(K key) = 0;
};

#endif

映射类 代码:

#ifndef __LINKEDLISTMAP_H__
#define __LINKEDLISTMAP_H__

#include "Map.h"

template <class K, class V>
class MapNode{
public:
	MapNode(K key = NULL, V value = NULL, MapNode<K, V> *next = NULL){
		m_key = key;
		m_value = value;
		this->next = next;
	}
public:
	K m_key;
	V m_value;
	MapNode<K, V> *next;
};

template <class K, class V>
class LinkedListMap : public Map<K, V>{
public:
	LinkedListMap(){
		head.m_key = NULL;
		head.m_value = NULL;
		head.next = NULL;
		m_size = 0;
	}
	int size(){
		return m_size;
	}
	bool isEmpty(){
		return m_size == 0;
	}
	//增加操作
	void add(K key, V value){
		if (!contains(key)){
			MapNode<K, V> *p = new MapNode<K, V>(key, value, head.next);
			head.next = p;
			m_size++;
		}
	}
	//删除操作
	V remove(K key){
		MapNode<K, V> *pre = &head;
		MapNode<K, V> *node = pre->next;
		while (node){
			if (key == node->m_key){
				pre->next = node->next;
				V res = node->m_value;
				delete node;
				m_size--;
				return res;
			}
			pre = node;
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
		return NULL;
	}
	//修改操作
	void set(K key, V value){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				node->m_value = value;
				return;
			}
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
	}
	//查找操作
	bool contains(K key){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				return true;
			}
			node = node->next;
		}
		return false;
	}
	V get(K key){
		MapNode<K, V> *node = head.next;
		while (node){
			if (key == node->m_key){
				return node->m_value;
			}
			node = node->next;
		}
		cout << "映射中不包含" << key << '!' << endl;
		return NULL;
	}
private:
	MapNode<K, V> head;
	int m_size;
};

#endif

猜你喜欢

转载自blog.csdn.net/qq_35481167/article/details/84027332
今日推荐