19,464
社区成员
发帖
与我相关
我的任务
分享
template<class TYPE2>
//插入尾
void Node<TYPE2>::PushEnd(char *name,int value)
{
ENListData<TYPE1> *pt =new ENListData<TYPE1>(name,value);
pt->next=head;//循环
size++;
if(head==NULL)//当连表为空时
{
head=pt;//当前节点及上一个节点,都指向同一个位置
last=head;
//当前节点
front=pt;
front->next=pt;
front->back=pt;
}
else
{
ENListData<TYPE1> *temp=front;
last->next=pt;
last=pt;
last->next=pt;
head->back = last;
front=pt;
front->next=head;
front->back=temp;
}
}
template<class TYPE2>
//打印指定节点DATA
void Node<TYPE2>::PrintfNodeTarget(char *name,int pos)
{
int Tcount=0;
ENListData<TYPE2> *temp=head;
if(pos>=0 && pos<size)
{
while(temp!=NULL)
{
if(Tcount==pos)
{
cout<<"-------target node data----------"<<endl;
cout<<"temp->value:"<<temp->value<<endl;
cout<<"temp->name:"<<temp->Name<<endl;
cout<<"back->value:"<<temp->back->value<<endl;
cout<<"back->name:"<<temp->back->Name<<endl;
cout<<"next->value:"<<temp->next->value<<endl;
cout<<"next->name:"<<temp->next->Name<<endl;
return;
}
temp=temp->next;
// cout<<"Tcount:"<<Tcount<<endl;
Tcount++;
if(temp==head || temp==NULL)
{
cout<<"PrintfNodeTarget(count)(找不到)\n";
break;
}
}
if(temp==NULL)
{
cout<<"list is null\n";
}
}
else if(name!=NULL)
{
while(temp!=NULL)
{
if(strcmp(name,temp->Name)==0)
{
cout<<"-------node data----------"<<endl;
cout<<"temp->value:"<<temp->value<<endl;
cout<<"temp->name:"<<temp->Name<<endl;
cout<<"back->value:"<<temp->back->value<<endl;
cout<<"back->name:"<<temp->back->Name<<endl;
cout<<"next->value:"<<temp->next->value<<endl;
cout<<"next->name:"<<temp->next->Name<<endl;
return;
}
temp=temp->next;
if(temp==head || temp==NULL)
{
cout<<"PrintfNodeTarget(name)(找不到)\n";
break;
}
}
if(temp==NULL)
{
cout<<"list is null\n";
}
}
else
{
cout<<"list is null\n";
}
}
//打印全部节点
template<class TYPE2>
void Node<TYPE2>::PrintfNode()
{
int Tcount=0;
ENListData<TYPE2> *temp=head;
cout<<"list size:"<<size<<endl;
while(temp!=NULL)
{
cout<<"value:"<<temp->value<<endl;
cout<<"name:"<<temp->Name<<endl;
temp=temp->next;
cout<<"Tcount:"<<Tcount<<endl;
Tcount++;
if(temp==head || temp==NULL)
{
cout<<"front->value:"<<front->value<<endl;
cout<<"front->next->value:"<<front->next->value<<endl;
cout<<"front->back->value:"<<front->back->value<<endl;
cout<<"last->value:"<<last->value<<endl;
cout<<"last->next->value:"<<last->next->value<<endl;
cout<<"last->back->value:"<<last->back->value<<endl;
cout<<"printf end\n";
break;
}
}
if(temp==NULL)
{
cout<<"(PrintfNode)list is null\n";
}
}
template<class TYPE1>
// 插入头
void Node<TYPE1>::PushBegin(char *name,int value)
{
ENListData<TYPE1> *pt =new ENListData<TYPE1>(name,value);
pt->next=head;//循环
size++;
if(head==NULL)//当连表为空时
{
head=pt;//当前节点及上一个节点,都指向同一个位置
last=head;
last->back=head;
//当前节点
front=pt;
front->next=pt;
front->back=pt;
}
else
{
ENListData<TYPE1> *temp=head;
temp->back=pt;
pt->back=last;
head=pt;
head->next = temp;
head->back=temp;
last->next=head;
front=head;
front->next=head->next;
front->back=last;
}
};
#endif
你搞会了这双向连表,8叉也是同一道理.
#ifndef ENLIST_H
#define ENLIST_H
#include"ENpublic.h"
//typedef int TYPE;
template<class TYPE>
class ENListData
{
public:
ENListData<TYPE>():next(0),back(0)
{
strcpy(Name,"");
value=0;
ObjectData=NULL;
}
ENListData<TYPE>(char *name,int val):next(0),back(0)
{
strcpy(Name,name);
value=val;
ObjectData=NULL;
}
public:
void setNew()
{
ObjectData=new TYPE;
}
void setDelete()
{
if(ObjectData!=NULL)
{
delete ObjectData;
ObjectData=NULL;
}
}
TYPE *getObjectData()
{
return ObjectData;
}
public:
ENListData<TYPE> *back;//上1节点;
ENListData<TYPE> *next;//下1节点
public:
TYPE *ObjectData;//存放数据
public:
char Name[100];//区分哪一个节点
int value;//值
};
template<class TYPE1>
class Node
{
public:
Node<TYPE1>():size(0)
{
head=NULL;//头节点
front=NULL;//当前节点
last=NULL;//最后一个节点
}
public:
// 插入头
void PushBegin(char *name,int value);//pass
//打印全部节点DATA
void PrintfNode();//pass
//打印指定节点DATA
void PrintfNodeTarget(char *name,int pos=-1);//pass
//插入尾
void PushEnd(char *name,int value);//pass
//delete all data
void ClearNode();//pass
//erase target note
void EraseNode(char *name,int pos=-1);//pass
//插入位置
//pos>=size时,pos=size-1
void InsertNode(char *name,int pos,ENListData<TYPE1> *newNode);//pass
//访问节点
ENListData<TYPE1> *GetNode(char *name,int pos=-1);
public:
ENListData<TYPE1> *head;//头节点(遍历的起始节点);
ENListData<TYPE1> *front;//当前节点;
ENListData<TYPE1> *last;//最后一个节点;
//插入节点的位置
int inserPos;
int size;
public:
};
//访问节点
template<class TYPE1>
ENListData<TYPE1> *Node<TYPE1>::GetNode(char *name,int pos)
{
if(name==NULL)
{
name="";
}
if(pos>=size)
{
pos=size-1;
}
ENListData<TYPE1> *temp=head;
int count=0;
while(temp!=NULL)
{
if(strcmp(name,temp->Name)==0 || pos==count)
{
return temp;
}
temp=temp->next;
if(temp==head)
{
cout<<"not get the node :"<<endl;
return NULL;
}
count++;
}
}
template<class TYPE1>
//插入位置
void Node<TYPE1>::InsertNode(char *name,int pos,ENListData<TYPE1> *newNode)
{
if(name==NULL)
{
name="";
}
if(pos>=size)
{
pos=size-1;
}
ENListData<TYPE1> *temp=head;
size++;
int count=0;
while(temp!=NULL)
{
if(strcmp(name,temp->Name)==0 || pos==count)
{
if(temp==last)
{
temp->back->next=newNode;//最后一个
newNode->back=temp->back;
newNode->next=temp;
temp->back=newNode;
return;
}
else if(temp==head)
{
ENListData<TYPE1> *pt =newNode;
pt->next=head;//循环
if(head==NULL)//当连表为空时
{
head=pt;//当前节点及上一个节点,都指向同一个位置
last=head;
last->back=head;
//当前节点
front=pt;
front->next=pt;
front->back=pt;
}
else
{
ENListData<TYPE1> *Htemp=head;
pt->back=last;
Htemp->back=pt;
head=pt;
head->next = Htemp;
front=head;
front->next=head->next;
front->back=last;
last->next=head;
}
return;
}
else
{
newNode->next=temp;
newNode->back=temp->back;
temp->back->next=newNode;
temp->back=newNode;
return;
}
}
temp=temp->next;
if(temp==head)
{
cout<<"not inser the node :"<<endl;
return;
}
count++;
}
}
//以名字作为删除标记时,pos=-1;
template<class TYPE1>
//erase target note
void Node<TYPE1>::EraseNode(char *name,int pos)
{
if(name==NULL)
{
name="";
}
ENListData<TYPE1> *temp=head;
int count=0;
while(temp!=NULL)
{
if(strcmp(name,temp->Name)==0 || pos==count)
{
if(temp==last)
{
head->back=temp->back;//头的上一个
temp->back->next=head;//倒数第2个
last=head->back;
if(front==temp)
{
front=temp->back;
}
cout<<"1:(erase)temp->value:"<<temp->value<<endl;
temp->setDelete();
delete temp;
temp=NULL;
size--;
return;
}
else if(temp==head)
{
head=temp->next;
last->next=head;
last->back=head->back->back;//当为最后一个时
if(front==temp)
{
front=temp->next;
front->back=head;
}
cout<<"2:(erase)temp->value:"<<temp->value<<endl;
temp->setDelete();
delete temp;
temp=NULL;
size--;
return;
}
else
{
temp->back->next=temp->next;
temp->next->back=temp->back;
cout<<"3:(erase)temp->value:"<<temp->value<<endl;
temp->setDelete();
delete temp;
temp=NULL;
size--;
return;
}
}
temp=temp->next;
if(temp==head)
{
cout<<"(erase)not find the node :"<<endl;
return;
}
count++;
}
}
template<class TYPE1>
//delete all data
void Node<TYPE1>::ClearNode()
{
ENListData<TYPE1> *temp=head;
while(temp!=NULL)
{
if(temp==last)
{
cout<<"DELETE1 temp->value:"<<temp->value<<endl;
temp->setDelete();
delete temp;
temp=NULL;
size--;
head=NULL;
}
else
{
ENListData<TYPE1> *Ntemp=temp->next;
cout<<"DELETE2 temp->value:"<<temp->value<<endl;
temp->setDelete();
delete temp;
temp=NULL;
temp=Ntemp;
Ntemp=NULL;
size--;
}
if(temp==NULL)
{
// size=0;
cout<<"DELETE3 list is empty(size):"<<size<<endl;
break;
}
}
}