单链表模板

来源:互联网 发布:安知玉如意说的是什么 编辑:程序博客网 时间:2024/05/23 12:16

不多说,直接的贴代码:


<span style="font-size:18px;">#include <iostream>#include <cstdlib>using namespace std;///////////////////////线性表的定义部分////////////////////////////节点信息//每个节点的数据域可以是任意类型的值template<class T>struct TpNode{    T date;    TpNode *next;    TpNode(TpNode *p=NULL )                //TpNode *tp_idol = new TpNode;    {    next=p; }                         //创建一个空结点tp_idol    TpNode(const T& Elem ,TpNode *p=NULL ) //TpNode *tp_idol = new TpNode(value);    {    date=Elem; next = p; }            //创建一个date域为value的结点};//线性表的操作函数集合template<class T>class TpList{public :    TpList();                         //构造函数    TpList(TpList& L);                //构造函数    ~TpList();                        //析构函数    bool IsEmpty()const;              //判断链表是否为空,空返回true    TpNode<T> *Locate(int i);         //返回第i节点的地址    TpNode<T> *GetHead();             //返回投机诶单的地址    int Search(T x);                  //查找与数据x相匹配的第一个节点,并分返位置    int ListLength();                 //返回线性表的长度    void post_Insert(T x);            //在表尾插入一个数据域为x的节点    void pre_Insert(T x);             //表头插入一个数据域为x的节点    void Insert(int i, T x);          //在第i个节点前插入一个数据域为x的节点    void GetElem(int i, T &x);        //获得第i个节点的数据域    void Remove(int i, T &x);         //删除第i个节点,返回数据域x    void ClearList();                 //清空链表,保留表头    void SetData(int i, T x);         //修改第i个节点的据域为x    TpList& operator = (TpList& L);   //重载操作符等private :    TpNode<T>* head;};//////////////////////线性表函数的实现部分//////////////////////构造函数生成链表头template<class T>TpList<T>::TpList(){   head=new TpNode<T>;    if(!head){        cout << "存储分配错误!" << endl;        exit(0);    }    head->next=NULL;}//重载构造函数,生成一个与已存在单链表L相同的链表template<class T>TpList<T>::TpList(TpList& L){    head=new TpNode<T>;    if(!head){        cout << "存储分配错误!" << endl;        exit(0);    }    T value;    TpNode<T> *p=L.GetHead();    TpNode<T> *q=head;    while( p->next !=NULL ){        value=p->next->date;        q->next=new TpNode<T>(value);        q=q->next;    //q->date=value;        p=p->next;    }    q->next=NULL;}//析构函数template<class T>TpList<T>::~TpList(){    TpNode<T>* p;    while( head->next!=NULL ){        p=head->next;        head->next=p->next;        delete p;    }}//返回结点i的地址template<class T>TpNode<T> *TpList<T>::Locate(int i){    if( i<0 || i> ListLength() ){        cout << "不存在第"<<i<<"个节点" << endl;        return NULL;    }    TpNode<T>* p=head;    int f=0;    while(p!=NULL && f!=i){        p=p->next;        f++;    }    return p;}//返回头结点的地址template<class T>TpNode<T> *TpList<T>::GetHead(){    TpNode<T>* p=head;    return p;}//查找数据域为X的结点在链表中第一次出现的位置template<class T>int TpList<T>::Search(T x){    int f=1;    TpNode<T>* p=head->next;    while(f<=ListLength() && p!=NULL){        if(x==p->date)            return f;        f++;    }    return -1;}//判断链表是否为空template<class T>bool TpList<T>::IsEmpty()const{    if(head->next==NULL)        return true;    return false;}//返回链表的长度template<class T>int TpList<T>::ListLength(){    int le=0;    TpNode<T> *p=head;    while(p->next!=NULL){        le++;        p=p->next;    }    return le;}//在表尾插入一个数据域为value的节点template<class T>void TpList<T>::post_Insert(T value){    TpNode<T> *p =head;    TpNode<T> *q=new TpNode<T>(value);    if(!q){        cout << "存储分配错误!" << endl;        exit(0);    }    //q->date=x;    //q->next=NULL;    while(p->next !=NULL) p=p->next;        p->next =q;}//在表头插入一个数据域为value的节点template<class T>void TpList<T>::pre_Insert(T value){    TpNode<T> *q=new TpNode<T>(value);    if(!q){        cout << "存储分配错误!" << endl;        exit(0);    }    //q->date=x;    q->next=head->next;    head->next=q;}//在节点i前插入一个数据域为value的节点template<class T>void TpList<T>::Insert(int i, T value){    TpNode<T> *p=Locate(i-1);    if(p!=NULL){        TpNode<T> *q=new TpNode<T>(value);        if(!q){            cout << "存储分配错误!" << endl;            exit(0);        }        //q->date=value;        //q->next=NULL;        q->next=p->next;        p->next=q;    }else {        exit(0);    }}//返回节点i的数据域template<class T>void TpList<T>::GetElem(int i, T &value){    TpNode<T> *p=Locate(i);    if(p==NULL){        cout<<"访问的节点不存在!"<<endl;        exit(0);    }    value=p->date;}//将节点i删除,并返回数据域valuetemplate<class T>void TpList<T>::Remove(int i, T &value){    TpNode<T> *p=Locate(i-1);    if(p!= NULL){        TpNode<T> *q;        value=p->next->date;        q=p->next;        p->next=q->next;        delete q;    }    else{        exit(0);    }}//清空单链表,保留表头template<class T>void TpList<T>::ClearList(){    TpNode<T> *q;    TpNode<T> *p=head->next;    head->next=NULL;    while(p!=NULL){        q=p;        p=p->next;        delete q;    }}//将节点i的数据域修改为valuetemplate<class T>void TpList<T>::SetData(int i, T value){    TpNode<T> *p=Locate(i);    if(p!=NULL){        p->date=value;    }    else{        exit(0);    }}//重载等号template<class T>TpList<T>& TpList<T>::operator = (TpList<T>& X){    T value;    TpNode<T> *p = X.GetHead();               //被复制表的头结点地址    TpNode<T> *q  = head = new TpNode<T>;     //创建头结点    while(p->next != NULL){                   //逐个复制结点        value = p->next->date;        q->next = new TpNode<T>(value);       //q->next->date=value;        q = q->next;        p = p->next;    }    q->next = NULL;    return *this;                              //返回操作对象地址};typedef struct testNode{    int ll;    int kk;    double ff;}usb;    int main(){    usb a;    a.ll=4 ,a.kk=6 ,a.ff=10.0;    TpList<usb> LsA;    LsA.pre_Insert(a);    LsA.post_Insert(a);    LsA.pre_Insert(a);    LsA.Insert(2, a);    TpList<usb> LsB(LsA);    TpList<usb> LsC;    LsC=LsB;    cout<<LsC.ListLength()<<endl;    return 0;}</span>




0 0