#include "Student.h"
#include <iostream>
using namespace std;
template<class T>
class linkNode{
public:
linkNode(linkNode<T>* ptr=NULL){
link=ptr;
}
linkNode(T& x,linkNode<T>* ptr=NULL){
data=x;
link=ptr;
}
T* data;
linkNode* link;
};
template<class T>
class List:public linkNode<T>{
protected:
linkNode<T>* first;
public:
List();
List(T& x);
List(List<T>& L);
~List();
void makeEmpty();
int length();
linkNode<T>* getHead();
linkNode<T>* search(T& x);
linkNode<T>* locate(int i);
bool getData(int i,T& x);
void setData(int i,T& x);
bool insert(int i,T& x);
bool remove(int i,T& x);
bool isEmpty();
void sort();
List<T>&operator==(List<T>& L);
List<T>&reverse(List<T>& L);
};
template<class T>
List<T>::List(){
first=new linkNode<T>();
}
template<class T>
List<T>::List(T& x){
first=new linkNode<T>(x);
}
template<class T>
List<T>::List(List<T>& L){
List<T>* newList;
linkNode<T>* newNode;
linkNode<T>* current;
current=first->link;
newList->first=first;
while (current->link!=NULL)
{
newNode->data=current->data;
newNode->link=current->link;
current=current->link;
newNode=newNode->link;
}
}
template<class T>
List<T>::~List(){
makeEmpty();
}
template<class T>
void List<T>::makeEmpty(){
linkNode<T>* del;
while(first->link!=NULL){
del=first->link;
first->link=del->link;
delete del;
}
}
template<class T>
int List<T>::length(){
linkNode<T>* q;
int count=0;
while(q->link!=NULL){
count++;
q=q->link;
}
return count;
}
template<class T>
linkNode<T>* List<T>::getHead(){
return first->link;
}
template<class T>
linkNode<T>* List<T>::search(T& x){
linkNode<T>* current;
current=first->link;
while(current->link!=NULL){
if(current->data==x)
break;
current=current->link;
}
return current;
}
template<class T>
linkNode<T>* List<T>::locate(int i){
linkNode<T>* current;
current=first->link;
for (int j=1;j<i;j++)
{
current=current->link;
}
return current;
}
template<class T>
bool List<T>::getData(int i,T& x){
linkNode<T>* a;
a=locate(i);
if (a!=NULL)
{
x=a->data;
return true;
}
return false;
}
template<class T>
void List<T>::setData(int i,T& x){
locate(i)->data=x;
}
template<class T>
bool List<T>::insert(int i,T& x){
linkNode<T>* newNode=new linkNode<T>(x);
if(newNode==NULL)
cerr<<"error"<<endl;
linkNode<T>* current;
current=locate(i);
if(current==NULL)
return false;
else{
newNode->link=current->link;
current->link=newNode;
}
return true;
}
template<class T>
bool List<T>::remove(int i,T& x){
linkNode<T>* current;
linkNode<T>* del;
current=locate(i-1);
if(current==NULL)
return false;
del=current->link;
x=del->data;
current=del->link;
delete del;
return true;
}
template<class T>
bool List<T>::isEmpty(){
if(first->link==NULL)
return true;
else
return false;
}
void main(){
List<int> ls;
int t,k;
ls.insert(1,t);
ls.getData(1,k);
cout<<k<<endl;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。