用C++模板技术写的单链表
来源:互联网 发布:网络销售有没有三包 编辑:程序博客网 时间:2024/06/06 00:21
第一段应用C++类模板技术写的东西,并且用在现在的系统中,效果还不错,感觉代码还不够精简。
- //单链表模板类
- using namespace std;
- template <class ElemType>
- class CSingleLink{
- private:
- ElemType* phead; //头指针
- ElemType* ptail; //尾指针
- ElemType* pcurr; //当前指针
- int NodesNum; //节点数量
- int CurrPos; //当前指针的序号(第一个节点的序号为1)
- protected:
- public:
- CSingleLink();
- ~CSingleLink();
- int GetNNum(); //返回节点数量
- int GetIndex(); //返回当前指针的序号
- ElemType* First();
- ElemType* Next(); //返回当前节点的下一个节点的指针
- ElemType* Prev(); //返回当前节点的前一个节点的指针
- ElemType* MoveTo(int index); //返回序号为index的节点指针(index'svalue from 1)
- ElemType* Insert(int pos,ElemType* aNode);//向第pos个节点插入节点aNode(pos >= 1 )
- ElemType* Append(ElemType* aNode); //向单链表末尾追加一个节点aNode
- ElemType* Delete(int pos); //删除第pos个节点
- int Delete(ElemType* p); //删除某个节点
- void Clear(); //清除链表
- };
- template <class ElemType>
- CSingleLink<ElemType>::CSingleLink()
- {
- phead = NULL;
- ptail = NULL;
- pcurr = NULL;
- NodesNum = 0;
- CurrPos = 0;
- }
- template <class ElemType>
- CSingleLink<ElemType>::~CSingleLink()
- {
- Clear();
- }
- template <class ElemType>
- int CSingleLink<ElemType>::GetNNum(){
- return (NodesNum);
- }
- template <class ElemType>
- int CSingleLink<ElemType>::GetIndex(){
- return(CurrPos);
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::First(){
- if(NodesNum > 0){
- pcurr = phead;
- CurrPos = 1;
- return (pcurr);
- }
- else return NULL;
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::Next(){
- if(NodesNum > 0){
- pcurr = pcurr->pnext;
- if(pcurr != NULL)
- CurrPos++;
- return(pcurr);
- }
- else return NULL;
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::Prev(){
- if(NodesNum > 1){ //NodesNum = 1只有头结点
- ElemType* pN = phead;
- while(pN->next != pcurr)
- pN = pN->next;
- pcurr = pN;
- CurrPos--;
- return(pcurr);
- }
- else return NULL;
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::MoveTo(int index){
- if(NodesNum > 0 && index >0 && index <= NodesNum){
- pcurr = phead;
- CurrPos = 1;
- for(int i = 1;i < index; i++)
- this->Next();
- return(pcurr);
- }
- else return NULL;
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::Insert(int pos,ElemType* aNode){
- if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
- if(NodesNum == 0){
- phead = aNode;
- ptail = aNode;
- aNode->next = NULL;
- CurrPos = 1;
- NodesNum = 1;
- }
- else if(pos == 1){
- aNode->next = phead;
- phead = aNode;
- CurrPos = 1;
- NodesNum++;
- }
- else if(this->MoveTo(pos - 1) != NULL){
- aNode->next = pcurr->next;
- pcurr->next = aNode;
- pcurr = aNode;
- CurrPos = pos;
- NodesNum++;
- }
- pcurr = aNode;
- return(pcurr);
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::Append(ElemType* aNode){
- if(NodesNum == 0){
- phead = aNode;
- ptail = aNode;
- }
- else {//if(this->MoveTo(NodesNum) != NULL){
- ptail->pnext = aNode;
- ptail = aNode;
- }
- NodesNum++;
- aNode->pnext = NULL;
- pcurr = aNode;
- CurrPos = NodesNum;
- return (pcurr);
- }
- template <class ElemType>
- ElemType* CSingleLink<ElemType>::Delete(int pos){
- if(pos < 1 || pos > NodesNum || NodesNum == 0) return NULL;
- if(pos == 1){
- this->MoveTo(1);
- phead = phead->next;
- }else if(this->MoveTo(pos - 1) != NULL){
- pcurr->next = pcurr->next->next;
- }
- NodesNum--;
- CurrPos = pos - 1;
- return(pcurr);
- }
- template <class ElemType>
- int CSingleLink<ElemType>::Delete(ElemType* p){
- if(NodesNum > 0){
- int r = 1;
- pcurr = this->First();
- while(pcurr != NULL)
- {
- if(pcurr != p)
- {
- pcurr = this->Next();
- r++;
- }
- else break;
- }
- if(pcurr != NULL)
- {
- delete pcurr;
- NodesNum--;
- CurrPos = r - 1;
- return r;
- }else return NULL;
- }
- else return NULL;
- }
- template <class ElemType>
- void CSingleLink<ElemType>::Clear(){
- if(NodesNum > 0)
- {
- ElemType *aNode;
- for(int i = 0; i< NodesNum;i++){
- aNode = pcurr;
- delete aNode;
- if(pcurr != NULL)
- pcurr = pcurr->pnext;
- }
- }
- phead = NULL;
- ptail = NULL;
- pcurr = NULL;
- NodesNum = 0;
- CurrPos = 0;
- }
- 用C++模板技术写的单链表
- 大神写的高精度模板 C/C++
- javascript模板技术 界面显示可以用另外的方法来写了
- 写技术文档,要不要用文档模板?
- 用C为LUA写一个超迷你的模板引擎.
- 模板写的单链表(list)
- 用模板写的堆栈类
- 自己用C写的单链表
- 【C/C++相关】论程序员写技术博客的重要性
- 九九乘法表!(用C写的)
- 自己写的矩阵模板
- resume模板,写的不好
- 自己写的splay模板
- c++写模板的注意事项
- 【C++】traits技术与模板元编程
- [C++] 利用模板的模板参数实现单链表
- 写模板string类, 认识STL的模块分离策略----小话c++(7)
- poj 上用java写代码的模板
- 开拓新客户:20个招术全搞定
- [JSP] JSP中字符转换问题
- kshen转Perl、PHP、ASP、JSP技术比较
- 最近学习的XML。
- 测试管理精华
- 用C++模板技术写的单链表
- Unicode 的编码和实现
- ArcGIS几种数据格式(ZZ)
- Unicode编码,解释UCS、UTF、BMP、BOM等名词
- 海量数据库的查询优化及分页算法方案
- 对象序列化
- 恭喜自己....開始MOSS2007之旅~
- 海量数据库的查询优化及分页算法方案
- 蚊子的故事