單鏈表模板

不多說,直接的貼代碼:


<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刪除,並返回數據域value
template<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的數據域修改爲value
template<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>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章