C++单链表实现

#include<iostream>
using namespace std;
template <class T>
struct SLNode
{
    T Data;///数据域
    SLNode<T> * next;///指针域
    SLNode():next(NULL) {} ///构造函数
    SLNode(const T& item):Data(item),next(NULL) {}
};
template <class T>
class SLList
{
private:
    SLNode<T> * head;///表头指针
    SLNode<T> * currptr;///当前位置指针
    int length;
public:
    SLList(void);///构造函数,构造只有一个哨位结点的空表
    SLList(const T&);///构造函数
    ~SLList(void);///析构函数
    bool IsEmpty(void)const///const函数不能修改其数据成员
    {
        return head->next==NULL;   ///判空
    }
    int GetLength(void)const
    {
        return length;
    };///返回表的长度
    bool Find(int ,T&);///存取
    int Search(const T&);///查找
    bool Delete(int);///删除:按下标删除
    bool Insert(int,const T&);///插入
    void Print(void);///打印
};
///构造函数
template <class T>
SLList<T>::SLList()
{
    head=currptr=new SLNode<T>();///创建哨位结点
    length=0;
}
template <class T>
SLList<T>::SLList(const T& item)
{
    currptr=new SLNode<T>(item);///生成一个表结点
    head=new SLNode<T>();///生成哨位结点
    head->next=currptr;
    length=1;
}
///析构函数
template <class T>
SLList<T>::~SLList()
{
    while(IsEmpty())
    {
        currptr=head->next;
        head->next=currptr->next;
        delete currptr;
    }
    delete head;
}
///存取
template <class T>
bool SLList<T>::Find(int k,T& item)
{
    if(k<0||k>length-1||IsEmpty())
    {
        return false;
    }

    currptr=head;
    for(int i=0; i<k; ++i)
    {
        currptr=currptr->next;
    }
    item=currptr->Data;
    return true;

}

///查找
template <class T>
int SLList<T>::Search(const T& item)
{
    currptr=head;
    for(int i=0; i<length; ++i)
    {
        if(currptr->Data==item)
        {
            return i;
        }
        else
        {
            currptr=currptr->next;
        }
    }
    return -1;
}

///删除:删除第k个元素,成功返回true,否则返回false
template <class T>
bool SLList<T>::Delete(int k)
{
    if(k<0||k>length-1||IsEmpty())
        return false;

    currptr=head;
    for(int i=0; i<k-1; ++i)
    {
        currptr=currptr->next;
    }
    SLNode<T> *temp;
    temp=currptr->next;
    currptr->next=temp->next;
    delete temp;
    length--;
    return true;

}

///插入:在第k个借点后插入一个值为item的节点
template <class T>
bool SLList<T>::Insert(int k,const T& item)
{
    if(k<0||k>length)
    {
        cout<<"please input right position!"<<endl;
        return false;
    }
    SLNode<T> * temp=new SLNode<T>(item);
    currptr=head;
    for(int i=0; i<k; i++)
    {
        currptr=currptr->next;
    }

    temp->next=currptr->next;
    currptr->next=temp;
    length++;
}

///打印
template <class T>
void SLList<T>::Print()
{
    currptr=head->next;
    for(int i=0; i<length; ++i)
    {
        cout<<currptr->Data<<"->";
        currptr=currptr->next;
    }
    cout<<endl;
    return;
}

///实例
int main()
{
    SLList<int> list(1);
    list.Print();
    ///尾插,尾结点为第length个结点,在length结点后插
    for(int i=5; i<=8; ++i)
    {
        list.Insert(list.GetLength(),i);
    }
    list.Print();
    ///头插,哨位结点为0结点,在0结点后插
    for(int i=2; i<=4; ++i)
    {
        list.Insert(0,i);
    }
    list.Print();
    ///任意插:在中间结点后插入0
    list.Insert(list.GetLength()/2,0);
    list.Print();
    ///存取:将结点位置为3的data取出
    int num;
    list.Find(3,num);
    cout<<"NUM3:>>"<<num<<endl;
    ///查询:查询data为0的结点位置
    num=0;
    cout<<"POS0:>>"<<list.Search(num)<<endl;
    ///删除第一个结点,哨位结点为第0个结点
    list.Delete(1);
    list.Print();
    return 0;

}

  

原文地址:https://www.cnblogs.com/liujw2114/p/10410138.html