第一段应用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;
- }