单链表基础操作C++实现

Node.h:

template <class T> struct Node
{
    T val;
    Node<T>* next;
    Node(T nVal)
    {
        val = nVal;
        next = nullptr;
    }
    Node(void){}
};
#include "Node.h"
#include <iostream>

using namespace std;

template <class T> class LinkList
{
    int size;
public:
    Node<T>* head;
    Node<T>* tail;
    LinkList(void)
    {
        head = tail = nullptr;
        size = 0;
    }
    ~LinkList(void){ Clear(); }

    void Add(T val)
    {
        Node<T>* pNode = new Node<T>(val);
        if (head == nullptr)
        {
            head = pNode;
            tail = pNode;
        }
        else
        {
            tail->next = pNode;
            tail = pNode;
        }
        size++;
    }

    bool insertAt(int pos, T val)
    {
        Node<T>* pNode = nullptr;
        if (pos<0 || pos>size())
        {
            cout << "out of range" << endl;
            return false;
        }
        if (pos == size)
        {
            Add(val);
            return true;
        }
        else if (pos == 0)
        {
            pNode = new Node<T>(val);
            pNode->next = head;
            head = pNode;
        }
        else
        {
            Node<T>* pNode = GetPointerAt(pos - 1);
            Node<T>* newNode = new Node<T>(val);
            newNode->next = pNode->next;
            pNode->next = newNode;
        }
        size++;
        return true;
    }

    bool RemoveAt(int pos)
    {
        Node<T>* pNode = nullptr;
        if (size == 0)
        {
            cout << "list is empty" << endl;
            return false;
        }
        if (pos<0 || pos>size - 1)
        {
            cout << "out of range" << endl;
            return false;
        }
        if (size == 1)
        {
            Claer();
        }
        else
        {
            if (pos == 0)
            {
                pNode = head;
                head = head->next;
                delete pNode;
            }
            else
            {
                Node<T>* pPreNode = GetPointerAt(pos - 1);
                pNode = pPreNode->next;
                pPreNode->next = pNode->next;
                delete pNode;
                if (pos == size - 1)
                    tail = pPreNode;
            }
        }
        size--;
        return true;
    }

    T GetHeadVal()
    {
        if (size == 0)
        {
            cout << "list is empty" << endl;
            return NULL;
        }
        return head->val;
    }

    T GetTailVal()
    {
        if (size == 0)
        {
            cout << "list is empty" << endl;
            return NULL;
        }
        return tail->val;
    }

    int Find(T val)
    {
        int index = 0;
        Node<T>* ip = head;
        while (ip != nullptr)
        {
            if (ip->val == val)
                return index;
            ip = ip->next;
            index++;
        }
        return -1;
    }

    bool IsEmpty()
    {
        return size == 0 ? true : false;
    }

    int Size(){ return size; }

    void Clear()
    {
        while (head != nullptr)
        {
            Node<T>* tmp = head->next;
            delete head;
            head = tmp;
        }
        tail = nullptr;
        size = 0;
    }

    private:
        Node<T>* GetPointerAt(int pos)
        {
            Node<T>* pNode = nullptr;
            if (pos<0 || pos>size - 1)
                cout << "out of range" << endl;
            else
            {
                pNode = head;
                for (int i = 1; i <= pos; i++)
                    pNode = pNode->next;
            }
            return pNode;
        }
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值