C++实现双链表

源码:

#include <iostream>
using namespace std;

// 定义模板类节点
template <typename T>
class Node
{
public:
    T data; // 存储数据
    Node* next; // 指向下一个节点的指针
    Node* prev; // 指向前一个节点的指针
    Node(T val) : data(val), next(nullptr), prev(nullptr)
    {

    } // 构造函数
};

// 定义模板类双向链表
template <typename T>
class DoublyLinkedList
{
private:
    Node<T>* head; // 头节点指针
    Node<T>* tail; // 尾节点指针
public:
    DoublyLinkedList() : head(nullptr), tail(nullptr)
    {

    } // 构造函数

    ~DoublyLinkedList()
    {
        Node<T>* current = head;
        while (current)
        {
            Node<T>* next = current->next;
            delete current;
            current = next;
        }
    } // 析构函数,释放所有节点内存

    // 插入节点到链表尾部
    void insert(T val)
    {
        Node<T>* newNode = new Node<T>(val); // 创建新节点
        if (!head)
        { // 如果链表为空
            head = tail = newNode; // 新节点作为头节点和尾节点
        }
        else
        {
            tail->next = newNode; // 将新节点链接到尾节点
            newNode->prev = tail;
            tail = newNode; // 更新尾节点
        }
    }

    // 修改指定位置的节点数据
    void modify(int index, T val)
    {
        Node<T>* temp = head;
        for (int i = 0; i < index; i++)
        { // 遍历到指定位置
            if (!temp)
            {
                cout << "索引超出范围" << endl;
                return;
            }
            temp = temp->next;
        }
        if (temp)
        {
            temp->data = val; // 修改数据
        }
        else
        {
            cout << "索引超出范围" << endl;
        }
    }

    // 删除指定位置的节点
    void remove(int index)
    {
        if (!head)
        {
            cout << "链表为空" << endl;
            return;
        }
        if (index == 0)
        { // 删除头节点
            Node<T>* temp = head;
            head = head->next;
            if (head)
            {
                head->prev = nullptr;
            }
            else
            {
                tail = nullptr; // 如果链表只有一个节点,删除后更新尾节点
            }
            delete temp;
            return;
        }
        Node<T>* temp = head;
        for (int i = 0; i < index; i++)
        { // 遍历到指定位置
            if (!temp)
            {
                cout << "索引超出范围" << endl;
                return;
            }
            temp = temp->next;
        }
        if (temp)
        {
            if (temp->next)
            {
                temp->next->prev = temp->prev;
            }
            else
            {
                tail = temp->prev; // 更新尾节点
            }
            temp->prev->next = temp->next;
            delete temp;
        }
        else
        {
            cout << "索引超出范围" << endl;
        }
    }

    // 查找指定值的节点,并返回所有匹配的索引
    void findAll(T val, int*& indices, int& count)
    {
        Node<T>* temp = head;
        int index = 0;
        count = 0;

        // 先遍历一遍链表以计算匹配值的个数
        while (temp)
        {
            if (temp->data == val)
            {
                count++;
            }
            temp = temp->next;
        }

        if (count == 0)
        {
            indices = nullptr;
            return;
        }

        // 分配数组存储匹配值的索引
        indices = new int[count];
        temp = head;
        index = 0;
        count = 0;

        // 再次遍历链表以记录匹配值的索引
        while (temp)
        {
            if (temp->data == val)
            {
                indices[count++] = index;
            }
            temp = temp->next;
            index++;
        }
    }

    // 显示链表中的所有节点
    void display()
    {
        Node<T>* temp = head;
        while (temp)
        {
            cout << temp->data << " "; // 打印每个节点的数据
            temp = temp->next;
        }
        cout << endl;
    }
};

int main()
{
    DoublyLinkedList<int> list;
    int n, value, index;

    cout << "请输入要添加的数字个数:";
    cin >> n;

    cout << "请输入这" << n << "个数字:" << endl;
    for (int i = 0; i < n; i++)
    {
        cin >> value;
        list.insert(value); // 插入数字
    }

    cout << "当前链表:" << endl;
    list.display(); // 显示链表

    cout << "请输入要修改的节点是第几个和新值:" << endl;
    cin >> index >> value;
    list.modify(index - 1, value); // 修改节点
    list.display(); // 显示链表

    cout << "请输入要删除的节点是第几个:" << endl;
    cin >> index;
    list.remove(index - 1); // 删除节点
    list.display(); // 显示链表

    cout << "请输入要查找的值:" << endl;
    cin >> value;

    int* indices;
    int count;
    list.findAll(value, indices, count); // 查找所有匹配的节点

    if (count > 0)
    {
        cout << "找到值 " << value << " 在第: ";
        for (int i = 0; i < count; i++)
        {
            cout << indices[i]+1 << " ";
        }
        cout << endl;
        delete[] indices; // 释放内存
    }
    else
    {
        cout << "值 " << value << " 不在链表中" << endl;
    }

    system("pause");
    return 0;
}

  • 11
    点赞
  • 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、付费专栏及课程。

余额充值