链表的实现(类模板)

心不动则不痛 提交于 2020-03-06 07:22:38

上一篇文章的链表实现有点小问题,此处使用类模板再做描述,不过部分代码有所不同。

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

template<class T>
class LinkList;    //类的声明

template<class T>
class ListNode{   //由于结构体不太便于描述,故此处使用类来描述结点
public:
    friend class LinkList<T>;    //为了使LinkList类能访问该类的私有变量,应将其设为ListNode的友元类
    //friend void f();
private:
    T data;
    ListNode<T>* next;
};

template<class T>
class LinkList{
public:
    LinkList(){head = NULL;}   //构造函数,初始化链表
    ~LinkList(){};             //析构函数
    bool ListEmpty(){return head == NULL;}  
    int ListLength();          //求链表长度
    void CreateListF();        //头插法构造链表
    void CreateListR();        //尾插法构造链表
    void CreateList();         //带头结点的尾插法
    bool GetElem(int i,T& x);  //判断第i个结点是否存在,若存在,将其记录在x中
    int LocateElem(T x);       //求x所在的位置
    void InsertNode(int i,T x);//在第i个元素前插入x
    void DeleteNode(int i,T& x);//删除第i个元素,并将其记录在x中
    void PrintList();          //打印该链表
    //void f();
private:
    ListNode<T>* head;
};

template<class T>
void LinkList<T>::CreateListF()
{
    ListNode<T>* p = head,* s;
    T ch;
    ch = getchar();
    while(ch != '\n'){         //换行符作为结束标志
        s = new ListNode<T>;
        s -> data = ch;
        s -> next = p;
        p = s;
        ch = getchar();
    }
    head = p;
}

template<class T>
void LinkList<T>::CreateListR()
{
    ListNode<T>* s,* rear = NULL;
    T ch;
    ch =getchar();
    while(ch != '\n'){
        s = new ListNode<T>;
        s -> data = ch;
        if(head == NULL)
            head = s;
        else
            rear = s;
        ch = getchar();
    }
    rear -> next = NULL;
}

template<class T>
void LinkList<T>::CreateList()
{
    ListNode<T>* s,* rear = NULL;
    T ch;
    head = new ListNode<T>;
    rear = head;
    ch = getchar();
    while(ch != '\n'){
        s = new ListNode<T>;
        s -> data = ch;
        rear -> next = s;
        rear = s;
        ch = getchar();
    }
    rear -> next = NULL;
}

template<class T>
int LinkList<T>::ListLength()
{
    ListNode<T>* p = head -> next;
    int num = 1;
    while(p){
        p = p -> next;
        num ++;
    }
    return num;
}

template<class T>
bool LinkList<T>::GetElem(int i, T& x)
{
    int j = 1;
    ListNode<T>* p = head -> next;
    while(p && j < i){
        p = p -> next;
        j ++;
    }
    if(p){
        x = p -> data;
        return 0;
    }
    return -1;
}

template<class T>
int LinkList<T>::LocateElem(T x)
{
    int num = 0;
    ListNode<T>* p = head -> next;
    while(p && p -> data != x){
        num ++;
        p = p -> next;
    }
    if(p)
        return ++ num;
    else
        return -1;
}

template<class T>
void LinkList<T>::InsertNode(int i, T x)
{
    ListNode<T>* p = head,* s;
    int j = 0;
    while(p && j < i - 1){
        p = p -> next;
        j ++;
    }
    if(i < 1 || i > ListLength() + 1)
        return ;
    s = new ListNode<T>;
    s -> data = x;
    s -> next = p -> next;
    p -> next = s;
}

template<class T>
void LinkList<T>::DeleteNode(int i,T& x)
{
    ListNode<T>* p = head,* u;
    int j = 0;
    while(p && j < i- 1){
        p = p -> next;
        j ++;
    }
    if(i < 1 || i > ListLength() + 1)
        return;
    u = new ListNode<T>;
    u = p -> next;
    p -> next = u -> next;
    x = u -> data;
    delete u;
}

template<class T>
void LinkList<T>::PrintList()
{
    ListNode<T>* p = head -> next;
    while(p){
        cout<<p -> data<<" ";
        p = p -> next;
    }
    cout<<endl;
}

int main()
{
    LinkList<char>L;
    char ch;
    int k;
    L.CreateList();
    L.PrintList();
    if(L.GetElem(8,ch) == 0)
        cout<<"这个位置的元素是:"<<ch<<endl;
    else
        cout<<"没有这个序号!"<<endl;
    L.InsertNode(5,'t');
    cout<<"在位置5插入t之后的内容如下:\n";
    L.PrintList();
    cout<<"删除位置5后的内容如下:\n";
    L.DeleteNode(5,ch);
    L.PrintList();
    cout<<"被删除的元素是:"<<ch<<endl;
    ch = 'm';
    k = L.LocateElem(ch);
    if(k != -1)
        cout<<"元素"<<ch<<"所在位置为:"<<k<<endl;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!