📄 wlink.cpp
字号:
#include "stdafx.h"
#include "WLink.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LinkedList::LinkedList()
{
RootRec=NULL;
CurRec=NULL;
NumRecs=0;
CurRecNum=0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
LinkedList::~LinkedList()
{
For(CurRecNum,NumRecs)
{
LinkedListElement *Temp=CurRec->NextRec;
delete CurRec;
CurRec=Temp;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL LinkedList::Add(void *NewRec)
{
LinkedListElement* LLE= new LinkedListElement;
if (LLE==NULL) return false;
LLE->Rec=NewRec;
if (NumRecs)
{
LLE->PrevRec=CurRec;
LLE->NextRec=CurRec->NextRec;
CurRec->NextRec->PrevRec=LLE;
CurRec->NextRec=LLE;
CurRecNum++;
}
else
{
RootRec=LLE;
LLE->NextRec=LLE;
LLE->PrevRec=LLE;
}
CurRec=LLE;
NumRecs++;
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL LinkedList::Append(void *NewRec)
{
if (Size())
{
Root();
Prev();
}
return Add(NewRec);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* LinkedList::operator[](long Index)
{
if (NumRecs==0) return NULL;
if (NumRecs==1) return(CurRec->Rec);
Index%=NumRecs;
if (Index==CurRecNum+1) return(Next());
if (Index==CurRecNum) return(CurRec->Rec);
if (Index==0) return(Root());
if (Index+1==CurRecNum) return(Prev());
Root();
long I=NumRecs/2;
if (Index>I)
{
long StopVal=NumRecs-Index;
For(I,StopVal) Prev();
}
else For(I,Index) Next(); // search forwards
return (CurRec->Rec);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void LinkedList::DelCur()
{
if (NumRecs>1)
{
if (RootRec==CurRec) RootRec=CurRec->NextRec;
CurRec->NextRec->PrevRec=CurRec->PrevRec;
CurRec->PrevRec->NextRec=CurRec->NextRec;
LinkedListElement *Temp=CurRec->NextRec;
delete CurRec;
CurRec=Temp;
NumRecs--;
CurRecNum %= NumRecs;
}
else if (NumRecs==1)
{
NumRecs=0;
RootRec=NULL;
delete CurRec;
CurRec=NULL;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL LinkedList::Insert(void *NewRec)
{
LinkedListElement* LLE= new LinkedListElement;
if (LLE==NULL) return false;
LLE->Rec=NewRec;
if (NumRecs)
{
LLE->PrevRec=CurRec->PrevRec;
LLE->NextRec=CurRec;
CurRec->PrevRec->NextRec=LLE;
CurRec->PrevRec=LLE;
if (CurRecNum==0) RootRec=LLE; // insert before the root
}
else
{
RootRec=LLE;
LLE->NextRec=LLE;
LLE->PrevRec=LLE;
}
CurRec=LLE;
NumRecs++;
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* LinkedList::Last()
{
if (Size())
{
Root();
return Prev();
}
else return NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void *LinkedList::Next()
{
if (Size())
{
CurRecNum=(CurRecNum+1)%NumRecs;
CurRec=CurRec->NextRec;
return CurRec->Rec;
}
return NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* LinkedList::Pop()
{
Root();
void* P=Prev();
DelCur();
Prev();
return P;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* Queue::Pop()
{
void* P=Root();
DelCur();
return P;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void *LinkedList::Prev()
{
if (Size())
{
CurRecNum=(CurRecNum+NumRecs-1)%NumRecs;
// trying to avoid a 0-1 situation in an unsigned storage area
CurRec=CurRec->PrevRec;
return CurRec->Rec;
}
return NULL;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void *LinkedList::Root()
{
if (RootRec==NULL) return NULL;
CurRecNum=0;
CurRec=RootRec;
return CurRec->Rec;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -