You need to enable JavaScript to run this app. C++实现STL风格双向链表时[]运算符触发段错误问题求助
优惠活动
大模型
产品
解决方案
定价
更多

C++实现STL风格双向链表时[]运算符触发段错误问题求助

问题修复说明
核心错误原因
  • push_back逻辑错误:单次调用重复创建多个Node实例,导致链表节点的next、prev指针关系完全混乱,[]运算符遍历链表时访问野指针触发段错误。原代码中每次调用push_back最多会new 3次节点,实际只需要new 1次。
  • operator[]参数类型错误:索引值应为整型,原代码定义为T const index,如果模板实例化时T不是整型会出现逻辑错误;同时索引越界时函数没有返回值,属于未定义行为。
  • 拷贝构造函数逻辑错误:遍历指针指向了当前空对象的head,而非传入的待拷贝对象L的head,导致拷贝构造得到的永远是空链表。
  • 双向链表指针维护缺失:push_front操作没有给原有头节点的prev赋值,pop_back、pop_front没有做空链表判断,也没有处理删除后链表为空的边界场景,都可能触发野指针访问。
修复后的完整代码
#include <iostream>
#include <stdexcept>
using namespace std;

template<typename T>
class Node
{
    public:
        T data;
        Node *prev;
        Node *next;
        Node(T data, Node *prev = nullptr, Node *next = nullptr): data(data), prev(prev), next(next) {}

};

template<typename T>
class List
{
    public:
        T& operator[](int const index);
        List();
        List(const List&);
        ~List();
        bool empty();
        int size();
        void clear();
        void push_back(T data);
        void push_front(T data);
        void pop_back();
        void pop_front();
        void resize(int nums);
    private:
        int Size;
        Node<T> *tail;
        Node<T> *head;
};

template<typename T>
List<T>::List()
{
    Size = 0;
    head = nullptr;
    tail = nullptr;
}

template<typename T>
List<T>::List(const List & L)
{
    head = nullptr;
    tail = nullptr;
    Size = 0;
    Node<T> *temp = L.head;
    while(temp)
    {
        push_back(temp->data);
        temp = temp->next;
    }
}

template<typename T>
List<T>::~List()
{
    Node<T> *temp;
    while(head)
    {
        temp = head;
        head = head->next;
        delete temp;
    }
}

template<typename T>
T & List<T>::operator[](int const index)
{
    if(index < 0 || index >= Size) {
        throw out_of_range("索引超出链表范围");
    }
    Node<T> *curr = head;
    int counter = 0;
    while (curr)
    {
        if (counter == index)
        {
            return curr->data;
        }
        curr = curr->next;
        counter++;
    }
    // 理论上不会走到此处,仅作为兜底避免未定义行为
    throw out_of_range("索引超出链表范围");
}

template<typename T>
void List<T>::push_back(T data)
{
    Node<T>* newNode = new Node<T>(data, tail, nullptr);
    if (head == nullptr) {
        head = newNode;
    } else {
        tail->next = newNode;
    }
    tail = newNode;
    ++Size;
}

template<typename T>
void List<T>::push_front(T data)
{
    Node<T>* newNode = new Node<T>(data, nullptr, head);
    if (head != nullptr) {
        head->prev = newNode;
    } else {
        tail = newNode;
    }
    head = newNode;
    Size++;
}

template<typename T>
void List<T>::pop_back()
{
    if (Size == 0) return;
    Node<T> *temp = tail;
    tail = tail->prev;
    if (tail != nullptr) {
        tail->next = nullptr;
    } else {
        head = nullptr;
    }
    delete temp;
    Size--;
}

template<typename T>
void List<T>::pop_front()
{
    if (Size == 0) return;
    Node<T> *temp = head;
    head = head->next;
    if (head != nullptr) {
        head->prev = nullptr;
    } else {
        tail = nullptr;
    }
    delete temp;
    Size--;
}

template<typename T>
void List<T>::clear()
{
    while (Size)
    {
        pop_back();
    }
}

template<typename T>
void List<T>::resize(int num)
{
    if (num > Size)
    {
        while (Size != num)
        {
            push_back(0);
        }
    }
    else{
        while (Size != num)
        {
            pop_back();
        }
    }
}

template<typename T>
bool List<T>::empty() {return Size == 0;}

template<typename T>
int List<T>::size() {return Size;}

int main()
{
    List<int> l;
    l.push_back(1234);
    l.push_back(124);
    l.push_back(10);
    l.push_front(1);

    for (int i = 0; i < l.size(); i++)
    {
        cout<<l[i]<<" ";
    }

    return 0;
}

修复后运行将输出1 1234 124 10,无段错误问题。

内容的提问来源于stack exchange,提问作者timaracov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 01:36:03