数据结构-双链表

// nodelist.h
#ifndef NODELIST_H
#define NODELIST_H

template<typename Type> class DoublyList;

template<typename Type> class ListNode
{
private:
    friend class DoublyList<Type>;
    ListNode() : m_pprior(NULL),m_pnext(NULL){}
    ListNode(const Type item, ListNode<Type>* prior = NULL, ListNode<Type>* next = NULL)
        : m_data(item), m_pprior(prior), m_pnext(next){}
    ~ListNode()
    {
        m_pprior = NULL;
        m_pnext = NULL;
    }

public:
    Type GetData();

private:
    Type m_data;
    ListNode* m_pprior;
    ListNode* m_pnext;
};

template<typename Type> Type ListNode<Type>::GetData()
{
    return this->m_data;
}

#endif // NODELIST_H
// doublelist.h
#ifndef DOUBLELIST_H
#define DOUBLELIST_H

#include "nodelist.h"
#include <iostream>

template<typename Type> class DoublyList
{
public:
    DoublyList() : head(new ListNode<Type>())
    {
        head->m_pprior = head;
        head->m_pnext = head;
    }
    ~DoublyList()
    {
        MakeEmpty();
        delete head;
    }
public:
    void MakeEmpty();
    int Length();
    ListNode<Type>* Find(int n = 0);
    ListNode<Type>* FindData(Type item);
    bool Insert(Type item, int n = 0);
    Type Remove(int n = 0);
    Type Get(int n = 0);
    void Print();
private:
    ListNode<Type> *head;
};

template<typename Type> void DoublyList<Type>::MakeEmpty()
{
    ListNode<Type> *pIterator = head->m_pnext, *pdel;
    while(pIterator != head)
    {
        pdel = pIterator;
        pIterator = pIterator->m_pnext;
        delete pdel;
    }
    head->m_pnext = head;
    head->m_pprior = head;
}

template<typename Type> int DoublyList<Type>::Length()
{
    ListNode<Type> *pprior = head->m_pprior, *pnext = head->m_pnext;
    int count = 0;
    while(1)
    {
        if(pprior->m_pnext == pnext) // pprior == head
        {
            break;
        }
        if(pprior == pnext && pprior != head)
        {
            count++;
            break;
        }
        count += 2;
        pprior = pprior->m_pprior;
        pnext = pnext->m_pnext;
    }
    return count;
}

template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n)
{
    if(n < 0)
    {
        std::cout << "The n is out of boundary" << std::endl;
        return NULL;
    }
    ListNode<Type> *pcursor = head->m_pnext;
    for( int i = 0; i < n; i++)
    {
        pcursor = pcursor->m_pnext;
        if( pcursor == head)
        {
            std::cout << "The n is out of boundary" << std::endl;
            return NULL;
        }
    }
    return pcursor;
}

template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item)
{
    ListNode<Type> *pcursorP = head->m_pprior;
    ListNode<Type> *pcursorN = head->m_pnext;
    while(pcursorP->m_pnext != pcursorN && pcursorP != pcursorN)
    {
        if(pcursorP->m_data == item)
        {
            return pcursorP;
        }
        if(pcursorN->m_data == item)
        {
            return pcursorN;
        }
        pcursorP = pcursorP->m_pprior;
        pcursorN = pcursorN->m_pnext;
    }

        std::cout << "can't find the item you want to find" << std::endl;
        return NULL;
}

template<typename Type> bool DoublyList<Type>::Insert(Type item, int n)
{
    if(n<0)
    {
        std::cout << "The n is out of boundary" << std::endl;
        return 0;
    }
    ListNode<Type> *newnode = new ListNode<Type>(item), *pcursor = head;
    if(newnode == NULL)
    {
        std::cout << __FUNCTION__ << " error " << std::endl;
        exit(1);
    }
    for(int i = 0; i < n; i++)
    {
        pcursor = pcursor->m_pnext;
        if(pcursor == head)
        {
            std::cout << "The n is out of boundary" << std::endl;
            exit(1);
        }
    }

    newnode->m_pnext = pcursor->m_pnext;
    newnode->m_pprior = pcursor;
    newnode->m_pnext->m_pprior = newnode;
    pcursor->m_pnext = newnode;
    return 1;
}

template<typename Type> Type DoublyList<Type>::Remove(int n)
{
    if(n<0)
    {
        std::cout << "The n is out of boundary" << std::endl;
        exit(1);
    }

    ListNode<Type> *pcursor = head, *pdel = NULL;
    for(int i = 0; i < n; i++)
    {
        pcursor = pcursor->m_pnext;
        if(pcursor == head)
        {
            std::cout << "The n is out of boundary" << std::endl;
            exit(1);
        }
    }
    pdel = pcursor;
    pcursor->m_pprior->m_pnext = pdel->m_pnext;
    pcursor->m_pnext->m_pprior = pdel->m_pprior;
    Type tmp = pdel->m_data;
    delete pdel;
    return tmp;
}

template<typename Type> Type DoublyList<Type>::Get(int n)
{
    if(n<0)
    {
        std::cout << "The n is out of boundary" << std::endl;
        exit(1);
    }

    ListNode<Type> *pcursor = head;
    for(int i = 0; i < n; i++)
    {
        pcursor = pcursor->m_pnext;
        if(pcursor == head)
        {
            std::cout << "The n is out of boundary" << std::endl;
            exit(1);
        }
    }

    return pcursor->m_data;
}

template<typename Type> void DoublyList<Type>::Print()
{
    ListNode<Type> *pcursor = head;
    std::cout << "head";
    while(pcursor->m_pnext != head)
    {
        std::cout << "--->" << pcursor->m_pnext->m_data;
        pcursor = pcursor->m_pnext;
    }
    std::cout << "--->over" << std::endl << std::endl << std::endl;
}

#endif // DOUBLELIST_H
// main.cpp
#include <iostream>
#include "doublelist.h"

using namespace std;

int main(int argc, char *argv[])
{
    DoublyList<int> list;
    for(int i = 0; i < 20; i++)
    {
        list.Insert(i*3, i);
    }
    cout << "the length of the list is " << list.Length() << endl;
    list.Print();
    for(int i = 0; i < 5; i++)
    {
        list.Insert(3, i*3);
    }
    cout << "the length of the list is " << list.Length() << endl;
    list.Print();

    list.Remove(5);
    cout << "the length of the list is " << list.Length() << endl;
    list.Print();

    cout << list.FindData(54)->GetData() << endl;
    cout << "the third element is " << list.Get(3) << endl;

    list.MakeEmpty();
    cout << "the length of the list is " << list.Length() << endl;
    list.Print();

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值