单链表

本文介绍了一个简单的单链表管理类的实现,包括插入、删除节点等基本操作,并通过示例展示了如何使用该类进行链表操作。
#include <iostream>
using namespace std;
typedef int nodeType;
struct tlist
{
    nodeType data;
    tlist  * next;
    tlist();
    ~tlist();
};
tlist::tlist()
{
    data = -1;
    next = NULL;
}
tlist::~tlist()
{
    tlist * temp;
    while (next)
    {
        temp = next;
        next = next->next;
        delete temp;
    }
}
class tlistManager
{
public:
    tlistManager();
    ~tlistManager();
    bool    insert(nodeType num);
    bool    deleteNode(nodeType num,bool duoge=false);
    tlist * findNode(nodeType num);
    tlist * findNodeParent(nodeType num);
    void    print();
    int        getSize();
public:
    tlist * m_root;        //头结点
    tlist * m_curNode;  //最后一个结点
    int        length;        //长度
};
tlistManager::tlistManager()
{
    m_root = NULL;
    m_curNode = NULL;
    length = 0;
}

tlistManager::~tlistManager()
{
    delete m_root;
}

bool    tlistManager::insert(nodeType num)
{
    //链表为空时
    if (!m_curNode)
    {
        tlist *temp = new tlist;
        temp->data = num;
        m_root    = temp;
        m_curNode = temp;
    }else
    {
        tlist *temp = new tlist;
        temp->data = num;
        m_curNode->next = temp;
        m_curNode  = temp;
    }
    length++;
    return true;
}

//是否删除多个结点
bool    tlistManager::deleteNode(nodeType num,bool duoge)
{
    tlist * chrid = findNode(num);
    tlist * parent = findNodeParent(num);
    tlist * tempNode = NULL;

    int size = 0;
    while (chrid)
    {
        tempNode = chrid;
        if (parent)
        {
            parent->next = chrid->next;
            //如果是删除尾结点
            m_curNode = chrid == m_curNode? parent:m_curNode;
        }else
        {
            //如果是删除头结点
            m_root = m_root == chrid?chrid->next:m_root;
        }
        tempNode->next = NULL;
        delete tempNode;
        tempNode= NULL;
        length --;
        size++;
        //是否需要删除多个
        if (duoge)
        {
             chrid = findNode(num);
             parent = findNodeParent(num);
        }else
        {
            return true;
        }
    }
    if (size)
    {
        return true;
    }
    return false;
}

tlist * tlistManager::findNode(nodeType num)
{
    //返回第一个符合的结点
    tlist * temp = m_root;
    while (temp)
    {
        if (temp->data == num)
        {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

tlist * tlistManager::findNodeParent(nodeType num)
{
    //如果是头结点
    if (m_root->data == num)
    {
        return NULL;
    }
    tlist * temp = m_root;
    while (temp && temp->next)
    {
        if (temp->next->data == num)
        {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

void    tlistManager::print()
{
    tlist * temp = m_root;
    while (temp)
    {
        cout<<temp->data<<" ";
        temp = temp->next;
    }
    cout<<"链表总长度为:"<<length<<endl;
}

int        tlistManager::getSize()
{
    return length;
}

int main()
{
    tlistManager * m_list = new tlistManager;
    for (int i= 0;i<10;++i)
    {
        m_list->insert(i);
        m_list->insert(i);
    }
    m_list->print();
    m_list->deleteNode(0,true);
    m_list->deleteNode(1);
    m_list->deleteNode(2,true);
    m_list->deleteNode(7);
    m_list->deleteNode(8);
    m_list->deleteNode(9,true);
    m_list->print();
    for (int i= 11;i<20 ;++i)
    {
        m_list->insert(i);
    }
    m_list->print();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值