Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
template<typename Key ,typename E>
class Dictionary
{
private:
void operator = (const Dictionary&){}
Dictionary(const Dictionary&){}
public:
Dictionary(){}
virtual ~Dictionary(){}
virtual void clear() =0;
virtual void insert(const Key& k , const E& e) =0;
virtual E remove(const Key& k) =0;
virtual E removeAny() =0;
virtual E find(const Key& k) = 0;
virtual int size() =0;
};
#endif
UALdict.h
#ifndef UALDICT_H
#define UALDICT_H
#include<iostream>
#include"LList.h"
#include"KVpair.h"
#include"Dictionary.h"
using namespace std;
template<typename Key,typename E>
class UALdict : public Dictionary<Key , E>
{
private:
LList< KVpair<Key,E> >* list;
public:
UALdict(int sz = defaultSize)
{
list= new LList< KVpair<Key,E> >(sz);
}
~UALdict()
{
delete list;
}
void clear()
{
list->clear();
}
void insert(const Key& k ,const E& e)
{
KVpair<Key,E> temp(k,e);
list->insert(temp);
}
E remove(const Key& k)
{
E temp = find(k);
if(temp != NULL)
{
list->remove();
}
return temp;
}
E removeAny()
{
if(list->length() == 0)
{
cerr<<"Dictionary is empty now..."<<endl;
exit(-1);
}
list->moveToEnd();
list->prev();
KVpair<Key , E> temp = list->remove();
return temp.value();
}
E find(const Key& k)
{
for(list->moveToStart() ; list->currPos() < list->length() ; list->next())
{
KVpair<Key ,E> temp = list->getValue();
if(temp.key() == k)
{
return temp.value();
}
}
return NULL;
}
int size()
{
return list->length();
}
};
#endif
KVpair.h
#ifndef KVPAIR_H
#define KVPATR_H
template<typename Key,typename E>
class KVpair
{
private:
Key k;
E e;
public:
KVpair(){}
KVpair(Key kval,E eval)
{
k = kval;
e = eval;
}
KVpair(const KVpair& o)
{
k = o.k;
e = o.e;
}
void operator = (const KVpair& o)
{
k = o.k;
e = o.e;
}
Key key()
{
return k;
}
void setKey(Key ink)
{
k = ink;
}
E value()
{
return e;
}
};
#endif
留言列表