在这之前,先说一个概念:字典。
字典(dictionary)是由一些形如(key,value)的数对所组成的集合,其中key是关键字,value是与关键字key对应的值(另一种说法是,value是值,这个值所对应的关键字就是key)。任意两个数对,其关键字都不等。有关字典的一些基本操作如下:
确定字典是否为空
确定字典有多少数对
寻找一个指定了关键字的数对
插入一个数对
删除一个指定了关键字的数对
一个字典是数对的集合,每个数对都由一个词和它的值组成。一个词的值包括词的意思、发音、词源等。
比如字典中的单词date,在字典中的内容就是date,the point if time at which a transation or event takes place,其中,date就是关键字。
一个多重字典和上述的字典类似,不同的是,多重字典中,两个或多个数对可以具有相同的关键字。
下面是字典的具体实现:
#include
#include"sortedchain.h"
using namespace std;
int main(void)
{
sortedChain z;
pair p;
p.first = 2;p.second = 10;
z.insert(p);
p.first = 10;p.second = 50;
z.insert(p);
p.first = 6;p.second = 30;
z.insert(p);
p.first = 8;p.second = 40;
z.insert(p);
p.first = 1;p.second = 5;
z.insert(p);
p.first = 12;p.second = 60;
z.insert(p);
cout<
cout <
cout<second<
cout<second<
cout<second<
z.erase(1);
z.erase(2);
z.erase(6);
z.erase(12);
cout<
cout<
cout<
return 0;
}
/*
* 数值对节点的定义
* pairNode.h
*/
#ifndef PAIRNODE_H
#define PAIRNODE_H
#include
using namespace std;
template
struct pairNode
{
typedef pair pairType;
pairType element;
pairNode *next;
pairNode(const pairType& thePair):element(thePair) {}
pairNode(const pairType &thePair,pairNode* theNext)
:element(thePair){next = theNext;}
};
#endif // PAIRNODE_H
/*
* 字典的抽象类,定义了字典操作需要的基本函数
* 其中,K代表关键字,E代表值
* dictionary.h
*
*/
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include"pairnode.h"
#include"iostream"
using namespace std;
template//K代表关键字,E代表值
class dictionary
{
public:
virtual ~dictionary(){}
virtual bool empty() const = 0;//如果字典为空,返回true
virtual int size() const = 0;//返回字典中数值对的数量
//返回某关键字所对应的值
virtual pair* find(const K&) const = 0;
//删除某关键字对应的数值对
virtual void erase(const K&) = 0;
//将某对数值对插入字典中
virtual void insert(const pair&) = 0;
};
#endif // DICTIONARY_H
/*
*
*/
#ifndef SORTEDCHAIN_H
#define SORTEDCHAIN_H
#include
#include"dictionary.h"
#include"pairnode.h"
using namespace std;
template
class sortedChain : public dictionary
{
public:
sortedChain() {firstNode = NULL;dSize = 0;}
~sortedChain();
bool empty() const{return dSize == 0;}//有序链表是否为空
int size() const{return dSize;}//返回有序链表元素个数
pair* find(const K&) const;//查找K值对应的某个元素对
void erase(const K&);//删除K值对应的某个元素对
void insert(const pair&);//插入元素对
void output(ostream& out) const;//输出链表元素
protected:
pairNode* firstNode;//指向链表中的第一个节点
int dSize;//字典中元素的个数,一个元素等于一个数对
};
template
sortedChain::~sortedChain()//析构函数,删除所有节点
{
while(firstNode != NULL)//顺次删除所有节点
{
pairNode* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template
pair* sortedChain::find(const K& theKey) const//查找theKey对应的数对
{
pairNode* currentNode = firstNode;
while(currentNode != NULL &&
currentNode->element.first != theKey)
currentNode = currentNode->next;
if(currentNode != NULL && currentNode->element.first == theKey)
return ¤tNode->element;
return NULL;
}
template
void sortedChain::insert(const pair& thePair)
{
pairNode *p = firstNode,
*tp = NULL;
while (p != NULL && p->element.first < thePair.first)
{
tp = p;
p = p->next;
}
if(p != NULL && p->element.first == thePair.first)
{
p->element.second = thePair.second;
return;
}
pairNode *newNode = new pairNode(thePair,p);
if(tp == NULL)
firstNode = newNode;
else
tp->next = newNode;
dSize++;
return;
}
template
void sortedChain::erase(const K& theKey)
{
pairNode *p = firstNode,
*tp = NULL;
while(p != NULL && p->element.first < theKey)
{
tp = p;
p = p->next;
}
if(p != NULL && p->element.first == theKey)
{
if(tp == NULL)
firstNode = p->next;
else
tp->next = p->next;
delete p;
dSize--;
}
}
template
void sortedChain::output(ostream &out) const
{
for(pairNode* currentNode = firstNode;
currentNode != NULL;
currentNode = currentNode->next)
out <element.first<
<element.second<
}
template
ostream& operator<& x)
{
x.output(out);
return out;
}
#endif // SORTEDCHAIN_H