C++实现双链表

C++简单实现顺序表和单链表
双链表

#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

struct ListNode
{
    ListNode* _next;
    ListNode* _prev;
    DataType _data;

    ListNode(DataType x)
        :_next(NULL)
        , _prev(NULL)
        , _data(x)
    {}
};

class List
{
    typedef ListNode Node;
public:
    List()
        :_head(NULL)
        , _tail(NULL)
    {}
    List(const List& l)
        :_head(NULL)
        , _tail(NULL)
    {
        Copy(l);
    }
    List& operator=(const List& l)
    {
        Destory();
        Copy(l);
        return *this;
    }
    ~List()
    {
        Destory();
    }
    void Copy(const List& l)
    {
        Node* cur = l._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    void Destory()
    {
        if (_head)
        {
            Node* cur = _head;
            while (_head)
            {
                cur = _head;
                _head = _head->_next;
                delete cur;
            }
            _head = _tail = NULL;
        }
    }
    void PushBack(DataType x)
    {
        if (_tail == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node* tmp = new Node(x);
            _tail->_next = tmp;
            tmp->_prev = _tail;
            _tail = tmp;
        }
    }
    void PopBack()
    {
        Erase(_tail);
    }
    void PushFront(DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Insert(_head, x);
        }
    }
    void PopFront()
    {
        Erase(_head);
    }
    // 在pos的前面插入一个 
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if (pos == _head)
        {
            Node* tmp = new Node(x);
            tmp->_next = _head;
            _head->_prev = tmp;
            _head = tmp;
        }
        else
        {
            Node* cur = pos;
            Node* prev = cur->_prev;
            Node* tmp = new Node(x);
            prev->_next = tmp;
            tmp->_next = cur;

            cur->_prev = tmp;
            tmp->_prev = prev;
        }
    }
    void Erase(Node* pos)
    {
        assert(pos);
        Node* prev = pos->_prev;
        Node* next = pos->_next;

        if (prev == NULL && next == NULL)
        {
            assert(pos == _head);
            _head = _tail = NULL;
        }
        else if (prev == NULL)
        {
            _head = next;
            _head->_prev = NULL;
        }
        else if (next == NULL)
        {
            _tail = prev;
            _tail->_next = NULL;
        }
        else
        {
            prev->_next = next;
            next->_prev = prev;
        }

        delete pos;
    }

    Node* Find(DataType x)
    {
        Node* cur = _head;
        while (cur)
        {
            if (cur->_data == x)
                return cur;
            cur = cur->_next;
        }
        return NULL;
    }
    void Reverse()
    {
        Node* cur = _head;
        while (cur)
        {
            swap(cur->_prev, cur->_next);
            cur = cur->_prev;
        }

        swap(_head, _tail);
    }
    void Print()
    {
        Node* cur = _head;
        while (cur)
        {
            cout << cur->_data << " ";
            cur = cur->_next;
        }
        cout << endl;
    }

private:
    Node* _head;
    Node* _tail;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于双向链表的插入数据并自动排序的 C 代码示例: ```c #include <stdio.h> #include <stdlib.h> /* 双向链表结构体 */ typedef struct node { int data; struct node *next; struct node *prev; } Node; /* 插入数据并自动排序 */ void insert(Node **head, int data) { /* 创建新节点 */ Node *new_node = (Node*) malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; new_node->prev = NULL; /* 如果链表为空,则新节点为头节点 */ if (*head == NULL) { *head = new_node; return; } /* 头节点数据比新节点数据大,新节点插入头部 */ if ((*head)->data > data) { new_node->next = *head; (*head)->prev = new_node; *head = new_node; return; } /* 查找新节点插入位置 */ Node *cur = *head; while (cur->next != NULL && cur->next->data < data) { cur = cur->next; } /* 插入新节点 */ new_node->next = cur->next; new_node->prev = cur; if (cur->next != NULL) { cur->next->prev = new_node; } cur->next = new_node; } /* 打印链表 */ void print_list(Node *head) { Node *cur = head; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } printf("\n"); } /* 主函数 */ int main() { Node *head = NULL; insert(&head, 3); insert(&head, 1); insert(&head, 4); insert(&head, 2); insert(&head, 5); print_list(head); return 0; } ``` 在上述代码中,我们首先定义了双向链表的结构体,包括数据域 `data` 和指向前驱节点和后继节点的指针 `prev` 和 `next`。 然后,我们定义了一个 `insert` 函数来实现插入数据并自动排序。如果链表为空,则新节点为头节点;如果头节点数据比新节点数据大,则新节点插入头部;否则,我们遍历链表找到新节点的插入位置,并插入新节点。 最后,我们实现了一个 `print_list` 函数来打印链表,并在主函数中测试了我们的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值