链接列表C ,重载[],整体结构

linked list c++, overloading [], overall structure

本文关键字:结构 重载 列表 链接      更新时间:2023-10-16

你好,我写了链接列表,我正在寻找一些建议,我不应该做什么或应该避免什么或我能做些什么,我也遇到了麻烦超载[],我找不到解决方案。

我已声明:

    linked_list<int> *pointer=new linked_list<int>();

并用一些数字填充它,然后试图通过:

到达它们
    int something = pointer[0];

等。它不起作用,我收到消息:

    error C2440: 'initializing': cannot convert from 'linked_list<int>' to 'int'

我尝试了:

    int something = pointer->operator[](0);

它起作用。我不知道怎么了。有人可以帮我吗?

其余代码:

Node:
    template <typename variable_type>
    class node {
    private:
        variable_type object;
        node<variable_type> *next_node;
    public:
        node();
        node(const variable_type &new_object, node<variable_type> *new_next_node);
        ~node();
        void set_object(const variable_type &new_object);
        void set_next_node(node<variable_type> *new_next_node);
        variable_type &get_object();
        node<variable_type> *get_next_node();
       };
    #include "node.h"
    template<typename variable_type>
    node<variable_type>::node()
    {
        this->next_node = nullptr;
    }
    template<typename variable_type>
    node<variable_type>::node(const variable_type & new_object, 
        node<variable_type> * new_next_node)
    {
        this->object = new_object;
        this->next_node = new_next_node;
    }
    template<typename variable_type>
    node<variable_type>::~node()
    {
        //
    }
    template<typename variable_type>
    void node<variable_type>::set_object(const variable_type & new_object)
    {
        this->object = new_object;
    }
    template<typename variable_type>
    void node<variable_type>::set_next_node(node<variable_type> * new_next_node)
    {
        this->next_node = new_next_node;
    }
    template<typename variable_type>
    variable_type &node<variable_type>::get_object()
    {
        return this->object;
    }
    template<typename variable_type>
    node<variable_type> *node<variable_type>::get_next_node()
    {
        return this->next_node;
    }

linked_list:

#include "node.cpp"
    #include <iostream>
    template <typename variable_type2>
    class linked_list
    {
    protected:
        node <variable_type2> *head;
        int size;
    public:
    linked_list();
    ~linked_list();
    bool is_empty() const;
    int length() const;
    void insert(variable_type2 &new_object, const int index);
    variable_type2 &remove(const int index);
    void print() const;
    variable_type2 &operator[](const int index);
};
#include "linked_list.h"
template<typename variable_type2>
linked_list<variable_type2>::linked_list()
{
    this->head = nullptr;
    this->size = 0;
}
template<typename variable_type2>
linked_list<variable_type2>::~linked_list()
{
    if (!is_empty()) {
        do {
            node <variable_type2> *temp = this->head;
            this->head = temp->get_next_node();
            delete temp;
        } while (head);
    }
    this->size = 0;
}
template<typename variable_type2>
bool linked_list<variable_type2>::is_empty() const
{
    if (!this->head) {
        return true;
    }
    else {
        return false;
    }
}
template<typename variable_type2>
int linked_list<variable_type2>::length() const
{
    return this->size;
}
template<typename variable_type2>
void linked_list<variable_type2>::insert(variable_type2 &new_object, const int index)
{
    if (this->is_empty()) {
        node <variable_type2> *new_node = new node<variable_type2>(new_object, nullptr);
        this->head = new_node;
    }
    else if (index == 0) {
        node <variable_type2> *new_node = new node<variable_type2>(new_object, this->head);
        this->head = new_node;
    }
    else if (index <= this->length()) {
        node <variable_type2> *temp = this->head;
        for (int i = 1; i < index; ++i) {
            temp = temp->get_next_node();
        }
        node <variable_type2> *new_node = new node<variable_type2>(new_object, temp->get_next_node());
        temp->set_next_node(new_node);
    }
    else { //when index is out of range
        return;
    }
    ++(this->size);
}

template<typename variable_type2>
variable_type2 &linked_list<variable_type2>::remove(const int index)
{
    if (index == 0) {
        node <variable_type2> *node_to_remove = this->head;
        variable_type2 return_object = node_to_remove->get_object();
        this->head = this->head->get_next_node();
        delete node_to_remove;
        --(this->size);
        return return_object;
    }
    else if (index <= this->length()) {
        node <variable_type2> *temp = this->head;
        for (int i = 2; i < index; ++i) {
            temp = temp->get_next_node();
        }
        node <variable_type2> *node_to_remove = temp->get_next_node();
        temp->set_next_node(node_to_remove->get_next_node());
        variable_type2 return_object = node_to_remove->get_object();
        delete node_to_remove;
        --(this->size);
        return return_object;
    }
    else {
        //return;
}
template<typename variable_type2>
void linked_list<variable_type2>::print() const
{
    if (!is_empty()) {
        node <variable_type2> *temp = this->head;
        for (int i = 0; i < this->length(); ++i) {
            std::cout << temp->get_object() << " ";
            temp = temp->get_next_node();
        }
        std::cout << std::endl;
    }
}
template<typename variable_type2>
variable_type2 &linked_list<variable_type2>::operator[](const int index)
{
    if (!is_empty() && index < this->length()) {
        node <variable_type2> *temp = this->head;
        for (int i = 0; i < index; ++i) {
            temp = temp->get_next_node();
        }
        variable_type2 return_value = temp->get_object();
        return return_value;
    }
}

你有

linked_list<int> *pointer=new linked_list<int>();

这意味着pointer是一个实际的指针。真正的指针具有编译器中定义的operator[],该指针执行指针算术并导致结果。这意味着

int something = pointer[0];

相同
int something = *(pointer + 0);

为您提供了最初创建的linked_list<int>的引用,并且您不能像错误所说的那样将linked_list分配给int

pointer是指针时,pointer[0]不调用您在linked_list类中定义的operator[]。为此,您要么必须使用

int something = (*pointer)[0];

int something = pointer->operator[](0);

就像您想出的那样。或者,您可以完全摆脱指针,并具有

linked_list<int> list;

然后您可以像

一样使用它
list.some_function_call();
int foo = list[some_index];