双向循环链表的基本操作.doc
文本预览下载声明
双向循环链表的基本操作
#includeiostream
using namespace std;
struct DblNode
{
int data;
DblNode *rLink;
DblNode*lLink;
};
class DblList
{
public:
DblList();
~DblList();
bool IsEmpty();
int GetLength();
void Output();
void ClearList();
bool HeadInsert(int e);
bool TailInsert(int e);
bool Insert(int pos,int e);
int Locate(int e);
bool DeleteNode(int pos,inte);
bool Insert(int i,const int x,int d);
bool SetData(int pos,int e);
DblNode *GetNode(int pos);
void Meun();
private:
DblNode *first;
DblNode *last;
int Length;
};
#includeDblList.h
DblList::DblList()
{
first=new DblNode;
if(!first)
{
exit(-1);
}
first-rLink=first;
first-lLink=first;
last=first;
Length=0;
}
DblList::~DblList()
{
DblNode *p1=first;
DblNode *p2;
while(p1!=last)
{
p2=p1;
p1=p1-rLink;
delete p2;
}
delete last;
}
bool DblList::IsEmpty()
{
if(Length==0)
return true;
return false;
}
bool DblList::HeadInsert(int e)
{
DblNode *p=new DblNode;
if(!p)
return false;
p-data=e;
p-rLink=NULL;
p-lLink=NULL;
if(Length==0)
{
p-rLink=first;
p-lLink=first;
first-rLink=p;
first-lLink=p;
last=p;
}
else
{
p-rLink=first-rLink;
p-lLink=first;
first-rLink-lLink=p;
first-rLink=p;
}
Length++;
return true;
}
bool DblList::TailInsert(int e)
{
DblNode *p=new DblNode;
if(!p)
return false;
p-data=e;
p-rLink=NULL;
p-lLink=NULL;
last-rLink=p;
p-lLink=last;
last=p;
last-rLink=first;
first-lLink=last;
Length++;
return true;
}
bool DblList::Insert(int pos,int e)
{
if(pos=1)
{
return HeadInsert(e);
}
else if(posLength)
{
return TailInsert(e);
}
else
{
DblNode*p1=GetNode(pos-1);
DblNode*p2=GetNode(pos);
DblNode*p=new DblNode;
if(!p)return false;
p-data=e;
p-rLink=p2;
p-lLink=p1;
p2-lLink=p;
p1-rLink=p;
Length++;
return true;
}
}
bool DblList::DeleteNode(int pos,inte)
{
if(pos1||posLength)return false;
DblNode*p1=GetNode(pos-1);
DblNode*p2=GetNode(pos);
if(p2==last)
{
delete p2
显示全部