单链表的类的c++实现

#include<iostream>

using namespace std;
template <class T>
struct linkNode{
T data;
linkNode<T> *link;
linkNode(linkNode<T> *pr=NULL){link=pr;}
linkNode(const T& item,linkNode<T> *pr=NULL){
data=item;
link=pr;
}
};
template<class T>
class list
{
protected:
linkNode<T> *first;

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
{
if(first->link==NULL)
return true;
else
return false;
}
bool IsFull()const {return false;}
void Sort();
void input();
void output();
list<T>& operator=(list<T>& L);

};
template<class T>
list<T>::list(list<T>& L)
{
T value;
linkNode<T> *srcptr =L.getHead();
linkNode<T> *desptr =first =new linkNode<T>;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new linkNode<T>(value);
desptr=desptr->link;
srcptr=srcptr->link;

}
desptr->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 count=0;
while(p!=NULL)
{
p=p->link;
count++;
}
return count;

}
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!=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);
if(newNode==NULL)
{
cerr<<"内存分配错误"<<endl;
exit(1);
}
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>::Sort()
{

}
template<class T> //前插法
void list<T>::input()
{
linkNode<T> *newNode;
T val;
makeEmpty();
cin>>val;
while(val!=0)
{
newNode = new linkNode<T>(val);
if(newNode ==NULL){cerr<<"存储分配错误!"<<endl;exit(1);}
newNode->link=first->link;
first->link=newNode;
cin>>val;

}

/*linkNode<T> *newNode ,*last;//后插法
T val;
makeEmpty();
cin>>val;
last=first;
while(val!=a)
{
newNode=new linkNode<T>(val);
if(newNode==NULL)
{
cerr<<"存储分配失败"<<endl;
}
last->link=newNode;
last=newNode;
cin>>val;
}
last->link=NULL;
*/

}
template<class T>
void list<T>::output()
{
linkNode<T> *current =first->link;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->link;
}

}
template<class T>
list<T>& list<T>::operator=(list<T>& L)
{
T value;
linkNode<T> *srcptr =L.getHead();
linkNode<T> *desptr =first =new linkNode<T>;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
desptr->link=new linkNode<T>(value);
desptr=desptr->link;
srcptr=srcptr->link;

}
desptr->link=NULL;

return *this;

}
int main()
{
list<int> h;
h.input();
h.output();

return 0;
}

上一篇:node.js—创建、删除、追加文件等方法汇总


下一篇:FROM CSDN TO CNBLOGS