📄 tllist.h
字号:
#ifndef _TLListH
#define _TLListH
#include "TLink.h"
template <class T>
class TLList
{
private:
TLink<T> * Head;
TLink<T> * Tail;
TLink<T> * Fence;
int Size;
private:
void init()
{
Fence = Head = Tail = new TLink<T>;
Size = 0;
}
void removeall()
{
while(Head != 0)
{
Fence = Head;
Head = Head->next;
delete Fence;
}
Size = 0;
}
public:
__fastcall TLList();
__fastcall TLList(const int &);//生成多个节点的链表
__fastcall ~TLList();
int __fastcall GetSize(void);
void __fastcall Append(const T &);
void __fastcall Clear(void);//删除所有的有效节点(存放数据的节点)
bool __fastcall Remove(const int &);//删除某点的节点
T & __fastcall operator[](const int &);
const T & __fastcall operator[](const int &) const;
};
#ifdef LinkError
//---------------------------------------------------------------------------
template <class T>
__fastcall TLList<T>::TLList()
{
init();
}
//---------------------------------------------------------------------------
template <class T>
__fastcall TLList<T>::TLList(const int & num)
{
init();
for(int i = 0; i < num; i++)
{
Tail = Tail->next = new TLink<T>;
Size++;
}
}
//---------------------------------------------------------------------------
template <class T>
__fastcall TLList<T>::~TLList()
{
removeall();
}
//---------------------------------------------------------------------------
template <class T>
void __fastcall TLList<T>::Append(const T & element)
{
Tail = Tail->next = new TLink<T>(element, 0);
Size++;
}
//---------------------------------------------------------------------------
template <class T>
void __fastcall TLList<T>::Clear(void)
{
removeall();
init();
}
//---------------------------------------------------------------------------
template <class T>
bool __fastcall TLList<T>::Remove(const int & pos)
{
if((pos < 0) || (pos > Size-1))
throw "OutOfBounds";
Fence = Head;
for(int i = 0; i < pos; i++)
{
Fence = Fence->next;
}
TLink<T> * temp = Fence->next;
Fence->next = temp->next;
if(temp == Tail)
{
Tail = Fence;
}
delete temp;
Size--;
return true;
}
//---------------------------------------------------------------------------
template <class T>
int __fastcall TLList<T>::GetSize(void)
{
return Size;
}
//---------------------------------------------------------------------------
template <class T>
T & __fastcall TLList<T>::operator[](const int & pos)
{
if((pos < 0) || (pos > Size-1))
{
throw "OutOfBounds";
}
Fence = Head->next;
for(int i = 0; i < pos; i++)
{
Fence = Fence->next;
}
return Fence->Element;
}
//---------------------------------------------------------------------------
template <class T>
const T & __fastcall TLList<T>::operator[](const int & pos) const
{
if((pos < 0) || (pos > Size-1))
{
throw "OutOfBounds";
}
TLink<T> * temp = Head->next;
for(int i = 0; i < pos; i++)
{
temp = temp->next;
}
return temp->Element;
}
//---------------------------------------------------------------------------
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -