自己的C++列表类实现(插入函数)出现问题

Trouble with own C++ list class implementation (insert function)

本文关键字:函数 问题 插入 C++ 列表 实现 自己的      更新时间:2023-10-16

我即将实现我自己的基本数据结构。这是一类列表。我正在努力修复插入(int 数据(。

想法:每个元素都有一个值和 3 个指针:指针 1:头部:指向头部单元格指针 2:当前:指向当前单元格指针3:下一页: 指向邻居元素的结构

每当我们将新元素放入列表中时,我已经尝试了 next = &date。在此处输入代码'

  class list{
  private:
      typedef struct element{
          int wert;
          element *next;};
          element *current;
          element *head;
  public://constructur. 
      list()
          {head=new element; current=head; head->next=0;}
/*A new element with value is beeing inserted */
void insert (int value){
    next= &value;} 
};
 void insert (int value){
    struct element* new_node = (struct element*) malloc(sizeof(struct element));
    new_node->wert = value;
    new_node->next = head;
    head = new_node;
    }

现在它起作用了。

您将列表结构与节点结构混合在一起。对于列表,您需要这样的东西:

class list { 
    node* head;
}

然后对于列表中您需要的注释

class node {
    element* currentData;
    node* next;
}

因此,这里的节点组成了列表结构,而存储在列表中的数据具有类型"element"。