//ListNode.h
//-------------------------------------------------------------------------
template<class T> class ListNode{
T data;
ListNode<T>* link;
public:
ListNode():link(NULL){};
ListNode(T value):link(NULL),data(value){};
~ListNode(){};
void SetLink(ListNode<T>* next);
ListNode<T>* GetLink();
T& GetData();
};
//-------------------------------------------------------------------------
template<class T>
T& ListNode<T>::GetData()
{
return data;
}
//-------------------------------------------------------------------------
template<class T>
ListNode<T>* ListNode<T>::GetLink()
{
return link;
}
//-------------------------------------------------------------------------
template<class T>
void ListNode<T>::SetLink((ListNode<T>*next)
{
link=next;ios
}
//---------------------------------------------------this
//List.cppspa
#include<iostream>
#include"ListNode.h"
using namespace std;
template<class T> class List{指針
ListNode<T>* head;
ListNode<T>* tail;
public:
List();
~List(){};
bool AddTail(T value);
bool RemoveTail();
bool InsertAt(int index,T value);
bool RemoveAt(int index);
T& GetAt(int index);
bool IsEmpty();
int GetCount();
void RemoveAll();
ListNode<T>* GetHead();
ListNode<T>* GetTail();
void SetTail(ListNode<T>* newtail);
ListNode<T>* GetNodeAt(int index);
//ListNode<T>* GetCur();
//ListNode<T>* TowardCur();
};
/*------------------------------------------------------------------------
template<class T>
ListNode<T>* List<T>::TowardCur()
{索引
}
//------------------------------------------------------------------------
template<class T>
ListNode<T>* List<T>::GetCur()
{it
}
*/
//------------------------------------------------------------------------
template<class T>
ListNode<T>* List<T>::GetNodeAt(int index)
{//返回索引值處的指針
ListNode<T>* temp=head->GetLink();
if(index>GetCount()-1||index<0)
{
cout<<"wrong index position."<<endl;
break;
}
for(int i=0;i<index;i++)
{
temp->SetLink(temp->GetLink());
}
return temp;
}
//------------------------------------------------------------------------
template<class T>
void List<T>::SetTail(ListNode<T>* newtail)
{//添加新的尾節點
ListNode<T>* temp=head;
while(temp->GetLink()!=tail)
{
temp->SetLink(temp->GetLink());
}
temp->SetLink(newtail);
newtail->SetLink(NULL);
tail=newtail;io
}
//------------------------------------------------------------------------
template<class T>
ListNode<T>* List<T>::GetTail()
{//返回尾指針
return tail;
}
//------------------------------------------------------------------------
template<class T>
ListNode<T>* List<T>::GetHead()
{//返回頭指針
return head;
}
//------------------------------------------------------------------------
template<class T>
void List<T>::RemoveAll()
{//刪除鏈表中全部節點
ListNode<T>* temp;
while(head->GetLink()!=NULL)
{
temp=head->GetLink();
head->SetLink(temp->GetLink());
delete temp;
}
tail=head;
}
//------------------------------------------------------------------------
template<class T>
int List<T>::GetCount()
{//返回鏈表中節點的個數
int ListNum=0;
ListNode<T>* temp=head->GetLink();
while(head!=NULL)
{
ListNum++;
temp=temp->GetLink();
}
return ListNum;
}
//------------------------------------------------------------------------
template<class T>
bool List<T>::IsEmpty()
{//判空
if(tail==head)
{
return true;
}
else
{
return false;
}
//return head->GetLink()==NULL;
}
//------------------------------------------------------------------------
template<class T>
T& List<T>::GetAt(int index){
//返回索引值節點中的value
ListNode<T>* temp=head->GetLink();
if((index>GetCount()-1)||index<0)
{
cout<<"A wrong position."<<endl;
return false;
}
for(int i=0;i<index;i++)
{
if(temp==NULL||temp==tail)
{
cout<<"There are not many datas"<<endl;
}
temp=temp->GetLink();
}
return temp->GetData();class
}
//------------------------------------------------------------------------
template<class T>
bool List<T>::RemoveAt(int index)
{//按照索引值刪除節點
ListNode<T>* cur =head;
ListNode<T>* curPre=head;
if((index>GetCount()-1)||index<0)
{
cout<<"A wrong position."<<endl;
return false;
}
curPre=cur->GetLink();
while(index)
{
cur=cur->GetLink();
--index;
}
if(tail==curPre)
{
tail=cur;
}
cur->SetLink(curPre->GetLink());
if(curPre!=NULL)
{
return true;
}
else
return false;
/*for(int i=0;i<index;i++)
{
if(temp==NULL||temp==tail)
{
cout<<"There are not many datas"<<endl;
}
temp=temp->GetLink();
}
temp.SetLink(temp->GetLink()->GetLink());
return true;
*/stream
}
//------------------------------------------------------------------------
template<class T>
bool List<T>::InsertAt(int index,T value)
{//在指定位置插入
if(index>this->GetCount()-1||index<0)
{
cout<<"a wrong position."<<endl;
return false;
}
ListNode<T>* temp=head;List
while(index)
{
temp=temp->GetLink();
--index;
}
ListNode<T>* NewNode=new ListNode<T>(value);
NewNode.SetLink(temp->GetLink());
temp->SetLink(NewNode);
if(temp->GetLink()!=NULL)
{
return true;
}
else
return false;
}
//------------------------------------------------------------------------
template<class T>
bool List<T>::RemoveTail()
{//刪除表尾節點
ListNode<T>* temp=head;
while(temp->GetLink!=tail)
{
temp=temp->GetLink();
}
temp.link=NULL;
tail=temp;
return true;
//return RemoveAt(this->GetCount()-1);
}
//------------------------------------------------------------------------
template<class T>
bool List<T>::AddTail(T value)
{//向表尾加入新節點
ListNode<T>* temp=new ListNode<T>(value);
tail->SetLink(temp);
tail=tail->GetLink();
tail->SetLink(NULL);
if(tail!=NULL)
{
return true;
}
else
return true;
}
//------------------------------------------------------------------------
template<class T>
List<T>::~List()
{//一次刪除每個元素,而後刪除頭結點
ListNode<T>* temp;
while(head->GetLink()!=NULL)
{
temp=head->GetLink();
head->SetLink(temp->GetLink());
delete temp;
}
tail=head;
delete head;
}
//------------------------------------------------------------------------
template<class T>
List<T>::List()
{//建立表頭結點,並初始化head和tail
tail=head=new ListNode<T>;
tail->SetLink(NULL);
}
//------------------------------------------------------------------------
轉載請代表出處.