单链表 带头结点
带头结点的单链表就方便了许多,注意尾插要加一个last指针,locate函数带来许多便捷。
代码
#include <iostream>
using namespace std;
template<class T>
struct linknode
{
T data;
linknode<T> *link;
linknode(linknode<T> * ptr=NULL)
{
link=ptr;
}
linknode(const T& item,linknode<T> * ptr=NULL)
{
data=item;
link=ptr;
}
};
template <class T>
class List
{
public:
List()
{
first=new linknode<T>;
}
List(const T& x)
{
first=new linknode<T>(x);
}
List(List<T>& L);
~List()
{
makeempty();
}
void makeempty();
int length()const;
linknode<T>* gethead()const
{
return first;
}
linknode<T>* Search(T x);
linknode<T>* locate(int i);
bool getdata(int i,T& X) const;
void setdata(int i,T& X);
bool Insert(int i,T& x);
bool Remove(int i,T& x);
bool isempty()const
{
return first->link==NULL?true:false;
}
bool isfull()const
{
return false;
}
void Sort();
void input();
void output();
List<T>& operator=(List<T>& L);
void inputfront(T endtag);
void inputrear(T endtag);
protected:
linknode<T> *first;
};
template<class T>
List<T>::List(List<T> &L)
{
T value;
linknode<T>* srcptr=L.gethead();
linknode<T>* destptr=first=new linknode<T>;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
destptr->link=new linknode<T>(value);
destptr=destptr->link;
srcptr=srcptr->link;
}
destptr->link=NULL;
}
template<class T>
void List<T>::makeempty()
{
linknode<T>* q;
while(first->link!=NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
}
template<class T>
int List<T>::length()const
{
linknode<T>* p=first->link;
int num=0;
while(p!=NULL)
{
p=p->link;
num++;
}
return num;
}
template<class T>
linknode<T>* List<T>::Search(T x)
{
linknode<T>*current=first->link;
while(current!=NULL)
if(current->data==x) break;
else current=current->link;
return current;
}
template<class T>
linknode<T> *List<T>::locate(int i)
{
if(i<0) return NULL;
linknode<T>* current=first;
int k=0;
while(current->link!=NULL&&k<i)
{
current=current->link;
k++;
}
return current;
}
template<class T>
bool List<T>::getdata(int i,T&x)const
{
if(i<=0) return NULL;
linknode<T> * current=locate(i);
if(current==NULL) return false;
else
{
x=current->data;
return true;
}
}
template<class T>
void List<T>::setdata(int i,T&x)
{
if(i<=0) return ;
linknode<T> * current=locate(i);
if(current==NULL) return ;
else current->data=x;
}
template<class T>
bool List<T>::Insert(int i,T&x)
{
linknode<T> * current=locate(i);
if(current==NULL) return false;
linknode<T> * newnode=new linknode<T> (x);
newnode->link=current->link;
current->link=newnode;
return true;
}
template<class T>
bool List<T>::Remove(int i,T&x)
{
linknode<T> * current=locate(i-1);
if(current==NULL||current->link==NULL) return false;
linknode<T> * del=current->link;
current->link=del->link;
x=del->data;
delete del;
return true;
}
template<class T>
void List<T>::output()
{
linknode<T> * current=first->link;
while(current!=NULL)
{
cout<<current->data<<endl;
current=current->link;
}
}
template<class T>
List<T>& List<T>::operator=(List<T>&L)
{
T value;
linknode<T> * srcptr=L.gethead();
linknode<T> * destptr=first=new linknode<T>;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
destptr->link=new linknode<T>(value);
destptr=destptr->link;
srcptr=srcptr->link;
}
destptr->link=NULL;
return *this;
};
template<class T>
void List<T>::inputfront(T endtag)
{
linknode<T> * newnode;
T val;
makeempty();
cin>>val;
while(val!=endtag)
{
newnode=new linknode<T>(val);
newnode->link=first->link;
first->link=newnode;
cin>>val;
}
}
template<class T>
void List<T>::inputrear(T endtag)
{
linknode<T> * newnode,*last;
T val;
makeempty();
cin>>val;
last=first;
while(val!=endtag)
{
newnode=new linknode<T>(val);
last->link=newnode;
last=newnode;
cin>>val;
}
last->link=NULL;
}
int main()
{
return 0;
}