用无序链表实现字典ADT(C++描述)
来源:互联网 发布:阿里算法工程师薪资 编辑:程序博客网 时间:2024/06/04 18:03
#include <iostream>
using namespace std;
class empty ...{
};
//sngly-linked list node
template <class Elem> class Link ...{
public:
Elem element; //value for this node
Link *next;
Link(const Elem& elemval, Link * nextval =NULL)
...{element = elemval; next = nextval; }
Link(Link* nextval = NULL) ...{ next = nextval; }
};
//linked list implementation
template <class Elem> class LList: public Link<Elem> ...{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init()...{
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}
public:
void removeall() ...{
while(head != NULL) ...{
fence = head;
head = head->next;
delete fence;
}
}
LList(int size = 10) ...{init(); }
~LList() ...{removeall();}
void clear() ...{removeall();init(); }
bool insert (const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart()
...{
//cout<<"in setStart rightcnt="<<rightcnt<<";leftcnt="<<leftcnt<<endl;
fence = head ; rightcnt += leftcnt; leftcnt = 0;
//cout<<"at the end of setStart rightcnt="<<rightcnt<<";leftcnt="<<leftcnt<<endl;
}
void setEnd()
...{ fence = tail; leftcnt +=rightcnt; rightcnt = 0; }
void prev();
void next() ...{
if (fence != tail )
...{ fence = fence->next; rightcnt--; leftcnt++; }
}
int leftLength() const ...{ return leftcnt; }
int rightLength() const ...{ return rightcnt; }
bool setPos (int pos);
bool getValue(Elem& it) const...{ //let e = fence's element
//cout<<"inLList out rightLength"<<rightLength()<<endl; //try
if(rightLength() == 0) return false;
it = fence->next->element;
return true;
}
void print() const;
};
template <class Elem>
bool LList<Elem>::insert(const Elem& item) ...{
fence->next = new Link<Elem>(item, fence->next);
if(tail == fence) tail = fence->next;
rightcnt++;
return true;
}
template <class Elem>
bool LList<Elem>::append(const Elem& item) ...{
tail = tail->next = new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
template <class Elem>bool LList<Elem>::remove(Elem& it) ...{
if(fence->next == NULL) return false;
it = fence->next->element;
Link<Elem>* ltemp = fence->next;
fence->next = ltemp->next;
if(tail == ltemp) tail = fence;
delete ltemp;
rightcnt--;
return true;
}
template <class Elem> void LList<Elem>::prev() ...{
Link<Elem>* temp=head;
if(fence == head) return;
while(temp->next != fence) temp = temp->next;
fence = temp;
leftcnt--; rightcnt++;
}
template <class Elem> bool LList<Elem>::setPos(int pos) ...{
if((pos < 0) || (pos > rightcnt+leftcnt)) return false;
fence = head;
for(int i = 0; i<pos; i++) fence = fence->next;
return true;
}
template <class Elem> void LList<Elem>::print() const ...{
Link<Elem>* temp = head;
cout<<"< ";
while(temp != fence)...{
cout<<temp->next->element<<" ";
temp = temp->next;
}
cout<<"| ";
while(temp->next != NULL) ...{
cout<<temp->next->element<<" ";
temp = temp->next;
}
cout<<"> ";
};
template <class Key ,class Elem>
class Dictionary ...{
public:
virtual void clear()= 0;
virtual bool insert(const Elem&) = 0;
virtual bool remove(const Key&, Elem&) = 0;
virtual bool removeAny(Elem&) = 0;
virtual bool find(const Key&, Elem&) const =0;
virtual int size() = 0;
};
template <class Key ,class Elem>
class LListDict: public Dictionary<Key,Elem> ...{
public:
LList<Elem>* list;
public:
LListDict( int size = 10 ) ...{ list = new LList<Elem>; }
~LListDict() ...{ list->removeall(); }
void clear() ...{ list->clear(); }
bool insert(const Elem& e) ...{ return list->append(e); }
bool remove(const Key& K, Elem& e) ...{
for(list->setStart(); list->getValue(e); list->next() )
if(K == e)...{ list->remove(e);return true; }
return false;
}
bool removeAny(Elem& e) ...{
if(size()==0)return false;
list->setEnd();
list->prev();
list->remove(e);
return true;
}
bool find(const Key& K,Elem& e)const ...{
//list->getValue(e);
//cout<<"e="<<e;
//cout<<"rightcnt="<<list->rightLength()<<endl;
for(list->setStart(); list->getValue(e); list->next() )
...{
//cout<<"k="<<K<<";e="<<e<<endl;
if(K == e )return true;
}//for
return false;
}
int size()
...{ return list->leftLength()+list->rightLength(); }
};
int main()
...{
LListDict<int, int>LDmanio;
//add link
for(int i = 0; i < 100; i++)
LDmanio.insert(i);
cout<<":::::::::::::::::::::::orginal list::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
int K=23,e;
cout<<"find()="<<LDmanio.find(K,e)<<endl;
//remove
LDmanio.remove(K,e);
cout<<":::::::::::::::::::::::remove done:::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
//removeany
LDmanio.removeAny(e);
cout<<"::::::::::::::::::::::removeany done:::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
//size()
cout<<"size()="<<LDmanio.size()<<endl;
//clear()
LDmanio.clear();
cout<<":::::::::::::::::::::::clear done:::::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
system("pause");
return 0;
}
using namespace std;
class empty ...{
};
//sngly-linked list node
template <class Elem> class Link ...{
public:
Elem element; //value for this node
Link *next;
Link(const Elem& elemval, Link * nextval =NULL)
...{element = elemval; next = nextval; }
Link(Link* nextval = NULL) ...{ next = nextval; }
};
//linked list implementation
template <class Elem> class LList: public Link<Elem> ...{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init()...{
fence = tail = head = new Link<Elem>;
leftcnt = rightcnt = 0;
}
public:
void removeall() ...{
while(head != NULL) ...{
fence = head;
head = head->next;
delete fence;
}
}
LList(int size = 10) ...{init(); }
~LList() ...{removeall();}
void clear() ...{removeall();init(); }
bool insert (const Elem&);
bool append(const Elem&);
bool remove(Elem&);
void setStart()
...{
//cout<<"in setStart rightcnt="<<rightcnt<<";leftcnt="<<leftcnt<<endl;
fence = head ; rightcnt += leftcnt; leftcnt = 0;
//cout<<"at the end of setStart rightcnt="<<rightcnt<<";leftcnt="<<leftcnt<<endl;
}
void setEnd()
...{ fence = tail; leftcnt +=rightcnt; rightcnt = 0; }
void prev();
void next() ...{
if (fence != tail )
...{ fence = fence->next; rightcnt--; leftcnt++; }
}
int leftLength() const ...{ return leftcnt; }
int rightLength() const ...{ return rightcnt; }
bool setPos (int pos);
bool getValue(Elem& it) const...{ //let e = fence's element
//cout<<"inLList out rightLength"<<rightLength()<<endl; //try
if(rightLength() == 0) return false;
it = fence->next->element;
return true;
}
void print() const;
};
template <class Elem>
bool LList<Elem>::insert(const Elem& item) ...{
fence->next = new Link<Elem>(item, fence->next);
if(tail == fence) tail = fence->next;
rightcnt++;
return true;
}
template <class Elem>
bool LList<Elem>::append(const Elem& item) ...{
tail = tail->next = new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
template <class Elem>bool LList<Elem>::remove(Elem& it) ...{
if(fence->next == NULL) return false;
it = fence->next->element;
Link<Elem>* ltemp = fence->next;
fence->next = ltemp->next;
if(tail == ltemp) tail = fence;
delete ltemp;
rightcnt--;
return true;
}
template <class Elem> void LList<Elem>::prev() ...{
Link<Elem>* temp=head;
if(fence == head) return;
while(temp->next != fence) temp = temp->next;
fence = temp;
leftcnt--; rightcnt++;
}
template <class Elem> bool LList<Elem>::setPos(int pos) ...{
if((pos < 0) || (pos > rightcnt+leftcnt)) return false;
fence = head;
for(int i = 0; i<pos; i++) fence = fence->next;
return true;
}
template <class Elem> void LList<Elem>::print() const ...{
Link<Elem>* temp = head;
cout<<"< ";
while(temp != fence)...{
cout<<temp->next->element<<" ";
temp = temp->next;
}
cout<<"| ";
while(temp->next != NULL) ...{
cout<<temp->next->element<<" ";
temp = temp->next;
}
cout<<"> ";
};
template <class Key ,class Elem>
class Dictionary ...{
public:
virtual void clear()= 0;
virtual bool insert(const Elem&) = 0;
virtual bool remove(const Key&, Elem&) = 0;
virtual bool removeAny(Elem&) = 0;
virtual bool find(const Key&, Elem&) const =0;
virtual int size() = 0;
};
template <class Key ,class Elem>
class LListDict: public Dictionary<Key,Elem> ...{
public:
LList<Elem>* list;
public:
LListDict( int size = 10 ) ...{ list = new LList<Elem>; }
~LListDict() ...{ list->removeall(); }
void clear() ...{ list->clear(); }
bool insert(const Elem& e) ...{ return list->append(e); }
bool remove(const Key& K, Elem& e) ...{
for(list->setStart(); list->getValue(e); list->next() )
if(K == e)...{ list->remove(e);return true; }
return false;
}
bool removeAny(Elem& e) ...{
if(size()==0)return false;
list->setEnd();
list->prev();
list->remove(e);
return true;
}
bool find(const Key& K,Elem& e)const ...{
//list->getValue(e);
//cout<<"e="<<e;
//cout<<"rightcnt="<<list->rightLength()<<endl;
for(list->setStart(); list->getValue(e); list->next() )
...{
//cout<<"k="<<K<<";e="<<e<<endl;
if(K == e )return true;
}//for
return false;
}
int size()
...{ return list->leftLength()+list->rightLength(); }
};
int main()
...{
LListDict<int, int>LDmanio;
//add link
for(int i = 0; i < 100; i++)
LDmanio.insert(i);
cout<<":::::::::::::::::::::::orginal list::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
int K=23,e;
cout<<"find()="<<LDmanio.find(K,e)<<endl;
//remove
LDmanio.remove(K,e);
cout<<":::::::::::::::::::::::remove done:::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
//removeany
LDmanio.removeAny(e);
cout<<"::::::::::::::::::::::removeany done:::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
//size()
cout<<"size()="<<LDmanio.size()<<endl;
//clear()
LDmanio.clear();
cout<<":::::::::::::::::::::::clear done:::::::::::::::::::::::::::"<<endl;
LDmanio.list->print(); //output the llist
system("pause");
return 0;
}
- 用无序链表实现字典ADT(C++描述)
- 用无序链表实现字典ADT(C++描述)
- 数据结构:顺序表的ADT(C语言描述)
- 无序符号表(链表实现,JAVA,算法(四))
- 优先队列:采用无序顺序表描述
- 无序链表实现符号表
- 无序链表实现优先队列
- 集合的无序链表实现
- 队列(Queue ADT)C语言实现
- C ADT 的实现
- 用ADT来描述学生考试成绩(仅供参考)
- 字典ADT
- 数据结构实现之无序符号表SequentialSearchST(使用链表实现)
- 含表头的链表ADT(C语言版)
- 字典的线性表描述
- C语言(4)--创建无序链表、释放链表
- 链表ADT C语言实现
- 链表ADT C语言实现
- 序列代码生成器
- if __name__ (python学习笔记一,注意前面是双下划线)
- FLASH调用网页上的JS方法,以及FLASH全屏播放的方法
- spring定时控制
- 明天去内蒙古
- 用无序链表实现字典ADT(C++描述)
- 大数运算(十)
- 在这里,我有了个家
- AjaxPanel自定义控件实现页面无刷新数据交互
- FPGA Adders: Performance Evaluation and Optimal Design
- Javascript代码总结
- 关于later discovery bug的一些想法
- PetShop 数据库 连接类
- java多线程经典实例