码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 双向链表操作总结

时间:2018-12-02 22:39:35      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:nta   ++   names   iostream   cpp   包含   div   node   tno   

第一、包含DoubleLinkNode 模板类和DoubleLinkList 模板类 

#pragma once
#include<iostream>
using namespace std;

template <typename T> class DoubleLinkList;//这里必须声明

template<typename T> class DoubleLinkNode {
public:
    T getData() {
        return m_data;
    }
private:    
    DoubleLinkNode() :m_pprior(NULL), m_pnext(NULL) {}
    DoubleLinkNode(const T item, DoubleLinkNode<T>* prior, DoubleLinkNode<T>* pnext) :m_data(item), m_pprior(prior), m_pnext(pnext)
    {}      

    ~DoubleLinkNode() {
        m_pprior = NULL;
        m_pnext = NULL;
    }
    friend class DoubleLinkList<T>; 
    DoubleLinkNode* m_pprior;
    DoubleLinkNode* m_pnext;
    T m_data;
};

template <typename T> class DoubleLinkList {
public:
    DoubleLinkList() {
        head = new DoubleLinkNode<T>();
    }
    ~DoubleLinkList() {
        delete head;
    }
    void cleanDoubleLink();
    bool insertNode(T data);
    bool insertNode(T data,int n);
    void printAll();
    bool deleteNode(T data);
    DoubleLinkNode<T>* findNode(int n);
    DoubleLinkNode<T>* findData(T data);
    bool deleteNodeByIndex(int i);
    T getData(int i);

private:
    friend class DoubleLinkNode<T>;
    DoubleLinkNode<T> *head;
};

template <typename T> void DoubleLinkList<T>::cleanDoubleLink() {
    DoubleLinkNode<T> *pmove = head->m_pnext,*pdel;
    while (pmove != NULL)
    {
        pdel = pmove;
        pmove = pmove->m_pnext;
        delete pdel;
    }  
    head->m_pnext = NULL;
}

template<typename T> bool DoubleLinkList<T>::insertNode(T data) {
    DoubleLinkNode<T> *pmove = head;
    while (pmove->m_pnext!=NULL)
    {
        pmove = pmove->m_pnext;
    }

    DoubleLinkNode<T> *newNode = new  DoubleLinkNode<T>(data,pmove,NULL);
    pmove->m_pnext = newNode;
    return true;
}

template<typename T> bool DoubleLinkList<T>::insertNode(T item,int n) {
    DoubleLinkNode<T> *pmove = head;
    for (int i = 1; i < n; i++)
    {
        pmove = pmove->m_pnext;
        if (pmove==NULL && i<n-1)
        {
            cout << "超出链表长度" << endl;
            return false;
        }
    }
    DoubleLinkNode<T> *newNode = new DoubleLinkNode<T>();
    newNode->m_data = item;

    newNode->m_pnext = pmove->m_pnext;
    newNode->m_pprior = pmove->m_pprior;
    if (pmove->m_pnext!=NULL)
    {
        pmove->m_pnext->m_pprior = newNode;
    }
    pmove->m_pnext = newNode;
    return true;
}

template<typename T> void DoubleLinkList<T>::printAll() {
    DoubleLinkNode<T>* pmove = head->m_pnext;
    while (pmove!=NULL)
    {
        cout << pmove->m_data << " ";
        pmove = pmove->m_pnext;        
    }
    cout << endl;
}

template<typename T> bool DoubleLinkList<T>::deleteNode(T item) {
    DoubleLinkNode<T>* pmove = head;
    while (pmove->m_pnext!=NULL)
    {
        if (pmove->m_data==item)
        {
            break;
        }
        pmove = pmove->m_pnext;
    }
    if (pmove==NULL)
    {
        cout << "没有找到该item" << endl;
        return false;
    }

    pmove->m_pprior->m_pnext = pmove->m_pnext;
    if (pmove->m_pnext!=NULL)
    {
        pmove->m_pnext->m_pprior = pmove->m_pprior;
    }    
    delete pmove;
    return true;
}

template<typename T> DoubleLinkNode<T>* DoubleLinkList<T>::findNode(int n) {
    if (n<1)
    {
        cout << "指定位置无效" << endl;
        exit(1);
    }
    DoubleLinkNode<T>* pmove = head;
    
    for (int i = 1; i <= n; i++)
    {
        pmove = pmove->m_pnext;
        if (pmove==NULL)
        {
            cout << "指定节点不存在!" << endl;
            exit(1);
        }
    }
    return pmove;
}

template<typename T> DoubleLinkNode<T>* DoubleLinkList<T>::findData(T item) {
    DoubleLinkNode<T>* pmove = head;
    while (pmove->m_pnext != NULL)
    {
        pmove = pmove->m_pnext;

        if (pmove == NULL)
        {
            cout << "没有找到所查节点" << endl;
            exit(1);
        }
        if (pmove->m_data == item)
        {
            break;
        }
        return pmove;
    }
}

template<typename T> bool DoubleLinkList<T>::deleteNodeByIndex(int n) {
    DoubleLinkNode<T>*pmove = head->m_pnext;
    for (int i = 1; i < n; i++)
    {      
        if (pmove == NULL && i < (n - 1))
        {
            cout << "没有找到该位置,删除失败" << endl;
            return false;
        }
        pmove = pmove->m_pnext;
    }   
    if (pmove->m_pnext!=NULL)
    {
        pmove->m_pprior->m_pnext = pmove->m_pnext;
        pmove->m_pnext->m_pprior = pmove->m_pprior;
    }
    else
    {
        pmove->m_pprior->m_pnext = NULL;
    }
    delete pmove;
    return true;
}

template<typename T> T DoubleLinkList<T>::getData(int n) {
    DoubleLinkNode<T>* pmove = head->m_pnext;
    for (int i = 1; i < n; i++)
    {
        if (pmove==NULL && i<(n-1))
        {
            cout << "没有找到" << endl;
            exit(1);
        }
        pmove = pmove->m_pnext;
    }

    if (pmove==NULL)
    {
        cout << "节点不存在" << endl;
    }
    return pmove->getData();
}

  

c++ 双向链表操作总结

标签:nta   ++   names   iostream   cpp   包含   div   node   tno   

原文地址:https://www.cnblogs.com/clc2008/p/10055328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!