c++ 双链表

#include <iostream>

template <class T>
struct Node
{
    T data;
    Node *pre;
    Node *next;
};
template <class  T>
class DoubleList
{
public:
    DoubleList();
    ~DoubleList();
public:
    int doublelink_Insert(DoubleList* ptr, int position, T merber);
    int doublelink_Erase(DoubleList * ptr, int position);
    void doublelink_Shows(DoubleList *ptr, int num);
private:
    Node<T> * root;
public:
    int length;
};
template<class T>
DoubleList<T>::DoubleList()
{
    root = new Node<T>;
    root->pre = NULL;
    root->next = NULL;
}
template<class T>
DoubleList<T>::~DoubleList()
{
}
template<class T>
int DoubleList<T>::doublelink_Insert(DoubleList * ptr, int position, T merber)
{
    Node<T> * node = new Node<T>;
    node->data = merber;
    if (ptr->length == 0) 
    {
        root->next = node;
        node->pre = node;
        node->next = node;
        ptr->length++;
        return 0;
    }
    else
    {
        if (position == 0) 
        {
            node->pre = root->next->pre;
            node->next = root->next;
            root->next->pre->next = node;
            root->next->pre = node;
            root->next = node;
            ptr->length++;
            return 0;
        }
        else
        {
            Node<T>* currnet = root->next;
            for (int i = 0; i < position; i++)
            {
                currnet = currnet->next;
            }
            node->next = currnet;
            node->pre = currnet->pre;
            currnet->pre->next = node;
            currnet->pre = node;
            ptr->length++;
            return 0;
        }
    }
}
template<class T>
int DoubleList<T>::doublelink_Erase(DoubleList * ptr, int position)
{
    if (ptr->length == 0) 
    {
        std::cout << " 已经删除完 "<< std::endl;
        
    }
    else
    {
        if (ptr->length == 1) 
        {
            ptr->root->next = NULL;
            ptr->length--;
        }
        else
        {
            Node<T> *deleteNode = root->next;
            for (int i = 0; i < position; i++)
            {
                deleteNode = deleteNode->next;
            }
            deleteNode->pre->next = deleteNode->next;
            deleteNode->next->pre = deleteNode->pre;
            delete deleteNode;
            ptr->length--;
        }
    }
    return 0;
}
template<class T>
void DoubleList<T>::doublelink_Shows(DoubleList * ptr, int num)
{
    Node<T> *current = root->next;
    for (int i = 0; i < num; i++)
    {
        std::cout << current->data << " ";
        current = current->next;
    }
    std::cout << std::endl;
}
int main()
{
    DoubleList<int>* doublelink_ptr = new DoubleList<int>;
    for (int i = 0;  i< 10; i++)
    {
        doublelink_ptr->doublelink_Insert(doublelink_ptr, 0, i);
    }
    doublelink_ptr->doublelink_Shows(doublelink_ptr, 20);
    doublelink_ptr->doublelink_Erase(doublelink_ptr, 2);
    doublelink_ptr->doublelink_Shows(doublelink_ptr, 20);
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值