实现了一个链表,具体见代码

//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;

}
//---------------------------------------------------

//List.cpp

#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()
{

}
*/
//------------------------------------------------------------------------
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;

}
//------------------------------------------------------------------------
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();

}
//------------------------------------------------------------------------
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;
  */

}
//------------------------------------------------------------------------
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;

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);
}
//------------------------------------------------------------------------

转载请表明出处.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值