鏈表模板類的定義

//------------------------------------------ClassLinkList.h------------------------------------------

template <class T> class linkList;

template <class T>
class Link
{
friend class linkList<T>;
private:
        T data;                                   // 用於保存結點元素的內容
         Link<T> * next;                                  // 指向後繼結點的指針
public:          
        Link(const T info,Link<T>* nextValue=NULL)
        {
            data=info;
            next=nextValue;
        }
        Link(Link<T>* nextValue)
        {
            next=nextValue;
        }
};

template <class T>
class linkList
{
private:
    Link<T> *head, *tail;                                  // 單鏈表的頭、尾指針
    Link<T> *setPos(const int i);                          // 返回線性表指向第p個元素的指針值
public:
    linkList(int s);                                      // 構造函數
    ~linkList();                                          // 析構函數
    bool isEmpty();                                       // 判斷鏈表是否爲空
    void clear();                                           // 將鏈表存儲的內容清除,成爲空表
    int length();                                            // 返回此順序表的當前實際長度
    bool append(const T value);                              // 在表尾添加一個元素value,表的長度增1
    bool insert(const int i, const T value);              // 在位置i上插入一個元素value,表的長度增1
    bool myDelete(const int i);                           // 刪除位置i上的元素,表的長度減 1
    bool getValue(const int i, T& value);                 // 返回位置i的元素值
    bool getPos(int &i, const T value);                      // 查找值爲value的元素,返回第1次出現的位置
    void output();
};

template <class T>                                        //輸出鏈表
void linkList<T>::output()
{
    int i;
    Link<T> *p=head;
    int size=length();
    cout<<"the length of linkList is "<<size<<endl;
    for(i=0;i<size;i++)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}

template <class T>                                       // 函數返回值是找到的結點指針,線性表的元素類型爲T
Link<T> * linkList <T>:: setPos(int i)
{
    if(i<0||i>=length())
    {
        cout<<"錯誤!所要查找的位置不存在!"<<endl;
        return false;
    }
    else
    {
        if(i==0)
            return head;
        else
        {
            int count = 0;
             Link <T> *p=head;
            while (p != NULL && count <i)
            {
                 p = p-> next;
                count++;
            }
            return p;                       // 指向第 i 結點,i=0,1,2,…,當鏈表中結點數小於i時返回NULL
        }
    }
}

template <class T>
linkList <T>::linkList(int s)                             //構造函數
{
    T value;
    int count=0;
    cout<<"You should input "<<s<<" datas!"<<endl
        <<"please input the first data(end with enter):";
    cin>>value;
    head=tail=new Link<T>(value);
    while(count<s-1)
    {
        cout<<"please input the next data(end with enter):";
        cin>>value;
        append(value);
        count++;
    }
}

template <class T>
linkList<T>::~linkList()                                 //析構函數
{
    Link<T> *p,*q;
    p=head;
    while(p!=NULL)
    {
        q=p->next;
        delete p;
        p=q;
    }
    cout<<"調用析構函數"<<endl;
}

template <class T>                                        //判斷鏈表是否爲空
bool linkList<T>::isEmpty()
{
    if(head==NULL)
        return true;
    else
        return false;
}

template <class T>                                       //清空鏈表
void linkList<T>::clear()
{
    Link<T> *p,*q;
    p=head;
    while(p!=NULL)
    {
        q=p->next;
        delete p;
        p=q;
    }
    head=NULL;
}


template <class T>                                       //返回當前鏈表的長度
int linkList<T>::length()
{
    Link<T> *p=head;
    int count=0;
    if(p==NULL)
        return count;
    else
    {
        while (p!=NULL)
        {
             p=p->next;
             count++;
        }
        return count;
    }
}

template <class T>
bool linkList<T>::append(const T value)                  //向鏈表尾端添加一個節點
{
    Link<T> *p=new Link<T>(value);
    tail->next=p;
    tail=p;
    return true;
}

template <class T>                                       //插入數據內容爲value的新結點作爲第i個結點
bool linkList<T> :: insert(const int i, const T value)
{
    Link<T> *p, *q;
                   
    if ((p = setPos(i -1)) == NULL)
    {                                                     // p 是第i個結點的前驅
        cout << " 非法插入點"<<endl;
        return false;
    }
    q = new Link<T>(value, p->next);
    p->next = q;
    if (p == tail)                                          // 插入點在鏈尾,插入結點成爲新的鏈尾
        tail = q;
    return true;
}

template <class T>                                       // 刪除位置爲i的節點
bool linkList<T>:: myDelete(const int i)
{
    Link<T> *p, *q;
   
    if ((p = setPos(i-1)) == NULL || p == tail)
    {                                                     // 待刪結點不存在,即給定的i大於當前鏈中元素個數
        cout << " 非法刪除點 " <<endl;
        return false;
    }
    q = p->next;                                          // q是真正待刪結點
    if (q == tail)
    {                                                      // 待刪結點爲尾結點,則修改尾指針
        tail = p;
        p->next = NULL;
        delete q;
    }
    if (q != NULL)
    {                                                      // 刪除結點q 並修改鏈指針           
        p->next = q->next;
        delete q;
    }
    return true;
}

template <class T>
bool linkList<T>::getValue(const int i, T& value)         //返回位置爲i的節點中的值
{
    Link<T> *p=setPos(i);
    if(p==NULL)
    {
        cout<<"錯誤!所要進行查詢的節點不存在。"<<endl;
        return false;
    }
    else
    {
        value=p->data;
        return true;
    }
}

template <class T>
bool linkList<T>::getPos(int& i,const T value)               //返回值爲value的第一個節點
{
    T data;
    for(i=0;i<length();i++)
    {
        getValue(i,data);
        if(data==value)
        {
            return true;
            break;
        }
    }
    cout<<"所要查找的值不存在!"<<endl;
    return false;
}

//------------------------------------------------main fun.cpp---------------------------------------------------
#include "iostream"
#include "ClassLinkList.h"

using namespace std;

int main()
{
    cout<<"--------------初始化鏈表---------------"<<endl;
    linkList<int> link(6);
    link.output();
    cout<<"-----------檢驗鏈表是否爲空------------"<<endl;
    if(link.isEmpty())
        cout<<"鏈表爲空!"<<endl;
    else
        cout<<"鏈表不爲空!"<<endl;
    cout<<"-----------鏈表末尾添加節點------------"<<endl;
    link.append(7);
    link.output();
    cout<<"--------在第六節點位置添加節點---------"<<endl;
    link.insert(6,1);
    link.output();
    cout<<"-------------刪除第三節點--------------"<<endl;
    link.myDelete(3);
    link.output();
    cout<<"-----------返回第六節點的值------------"<<endl;
    int value;
    link.getValue(6,value);
    cout<<"the value which you find is "<<value<<endl;
    link.output();
    cout<<"----返回所要查找的值出現的第一位置-----"<<endl;
    int position;
    link.getPos(position,1);
    cout<<"the value you find first appeaed in position "<<position<<endl;
    link.output();
    cout<<"----------------清空鏈表----------------"<<endl;
    link.clear();
    if(link.isEmpty())
        cout<<"鏈表爲空!"<<endl;
    else
        cout<<"鏈表不爲空!"<<endl;
    link.output();
  

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