c++实现双链表

1、双链表顾名思义是一种链表
但是它与单链表不同的是它的每一个节点都有两个指针,也就加上数据域和指针域包括三个部分,分别为val,pre指针,next指针
2、双链表的结构如下:
 
3、双链表的操作如下:
 
#include<iostream>
using namespace std;
class MyLinkedList {
public:
    struct DoublyListNode {
        int val;
        DoublyListNode* pre;
        DoublyListNode* next;
        DoublyListNode(int x) : val(x), next(NULL), pre(NULL)
        {}
    };
    DoublyListNode* head;
    int size;
    
     MyLinkedList() {
        this->head = new DoublyListNode(0);
        this->size = 0;
    }
    
    int get(int index) {
        if (index < 0 || index >= size)
            return -1;
        DoublyListNode* pos_node = head->next;
        for (int i = 0; i < index; i++)
        {
            pos_node = pos_node->next;
        }
        return pos_node->val;
    }
    void addAtHead(int val) {
        DoublyListNode* newnode = new DoublyListNode(val);
        newnode->next = this->head->next;
        newnode->pre = head;
        head->next = newnode;
        head->next->pre = newnode;
        this->size++;
    }
    void addAtTail(int val) {
        DoublyListNode* newnode = new DoublyListNode(val);
        DoublyListNode* pos_node = this->head;
        for (int i = 0; i < this->size; i++)
        {
            pos_node = pos_node->next;
        }
        pos_node->next = newnode;
        newnode->pre = pos_node;
        this->size++;
    }
    void addAtIndex(int index, int val) {
        if (index == this->size)
        {
            addAtTail(val);
        }
        else if (index > this->size)
        {
            return;
        }
        else if (index < 0)
        {
            addAtHead(val);
        }
        else
        {
            DoublyListNode* newnode = new DoublyListNode(val);
            DoublyListNode* pos_node = this->head;
            for (int i = 0; i < index; i++)
            {
                pos_node = pos_node->next;
            }
            newnode->next = pos_node->next;
            newnode->pre = pos_node;
            pos_node->next = newnode;
            pos_node->next->pre = newnode;
            this->size++;
        }
    }
    void deleteAtIndex(int index) {
        
        if (index >= this->size||index < 0)
        {
            return;
        }
        else
        {
            DoublyListNode* pos_node = this->head;
            for (int i = 0; i < index; i++)
            {
                pos_node = pos_node->next;
            }
            pos_node->next = pos_node->next->next;
            if(pos_node->next!=nullptr)
            pos_node->next->pre = pos_node;
            this->size--;
        }
    }
    void  DoublyListNode_print()
    {
            DoublyListNode* pos_node = this->head;
            for (int i = 0; i < this->size; i++)
            {
                pos_node = pos_node->next;
                cout << pos_node->val << " ";
            }
            cout << endl;
    }
};
int main()
{
    MyLinkedList* linkedList = new MyLinkedList();
    linkedList->addAtHead(1);
    linkedList->addAtHead(2);
   /* linkedList->addAtHead(2);
    linkedList->addAtTail(9);
    cout<<linkedList->get(2)<<endl;
    for (int i = 0; i < 10; i++)
    {
        linkedList->addAtIndex(i,i+1);
    }*/
    linkedList->deleteAtIndex(0);
    linkedList->DoublyListNode_print();
    system("pause");
    return 0;
}

  

 
posted @ 2022-09-04 09:13  铜锣湾陈昊男  阅读(12)  评论(0)    收藏  举报