【问题标题】:Sinlge Linked List, C++, problem with delete all and search functions单链表,C++,删除所有和搜索功能的问题
【发布时间】:2020-05-16 20:39:22
【问题描述】:

我试图实现一个单链表。但是,我对搜索和删除功能有疑问。我遇到分段错误,我不知道为什么。有人可以解释我,我做错了什么,以及如何改进这个代码工作?谢谢你

#include <iostream>

class T
{
private:
    float t;
public:
    T *next;
    T()
    {
        this->t = 0.0;
        this->next = NULL;
    }
    float getT()
    {
        return this->t;
    }
    T(float t, T* next)
    {
        this->t = t;
        this->next = next;
    }
    T(const T& tx)
    {
        this->t = tx.t;
        this->next = tx.next;
    }
    void print()
    {
        std::cout << this->t << "\n";
    }
};

class MyList
{
private:
    T *head;
public:
    T* add_T(T *x)
    {
        T *new_head = new T(*head);
        new_head -> next = head;
        head = new_head;
        return head;
    }
    void print()
    {
        for(T *curr = head; curr != NULL; curr = curr->next)
            curr->print();
    }
    void delete_all()
    {
        T *x = head;
        while (x!= NULL)
        {
            head = head->next;
            delete x;
            x = head;
        }
        head = NULL;
    }
    T * search_val(T * k)
    {
        if (this->head == NULL)
            return NULL;

        if (this->head->getT() == k->getT())
            return this->head;

        return search_val(this->head->next);
    }
};

int main()
{
    MyList ml;
    T a,b,c;

    ml.add_T(&a);
    ml.add_T(&b);
    ml.add_T(&c);
    ml.print();

    T *y = new T(3.14, NULL);
    T *x = ml.search_val(y);

    ml.delete_all();
    delete x,y;

    return 0;
}

【问题讨论】:

标签: c++ linked-list segmentation-fault singly-linked-list


【解决方案1】:

看起来您列出的 head 没有初始化(应该是 0)。 为什么你的T* add_T(T *x) 会创建一个新元素?它可能应该只是将给定元素插入到列表中,而不是创建一个新元素。如果你的 List 类有一个 T head; 你可以通过调用类似的东西来添加元素

T* next = t.head;
t.head = x;
x->next = next;

在这种情况下,销毁列表将是空的,因为列表元素来自堆。

【讨论】:

    【解决方案2】:
    class MyList
    {
    private:
        T *head;
    public:
        T* add_T(T *x)
        {
            T *new_head = new T(*head);
            new_head -> next = head;
            head = new_head;
            return head;
        }
    

    Q1:当MyList被构造时,head指向什么?
    A1:我们不知道(未定义)。

    Q2:为什么add_T()不使用x参数?
    A2:它实际上并没有添加x指向的元素,而是复制了当前的head

    修复这两个错误会让你走得更远。

    另请参阅:How to debug small programs

    【讨论】:

      【解决方案3】:

      您在main() 中处理节点的方式完全错误。列表之外的代码应该关注,而不是节点

      您没有初始化列表的 head 成员,因为 MyList 缺少默认构造函数。

      add_T() 完全忽略了它的 x 参数。相反,它制作了head 节点的副本,然后将该副本插入到列表的前面。

      delete_all() 已经delete'd 该节点之后,您正在尝试delete search_val() 返回的节点。

      试试类似的方法:

      #include <iostream>
      
      class MyNode
      {
      public:
          float data;
          MyNode *next;
      
          MyNode(float data = 0.0f, MyNode* next = NULL)
              : data(data), next(next)
          {
          }
      
          void print() const
          {
              std::cout << data << std::endl;
          }
      };
      
      class MyList
      {
      private:
          MyNode *head;
      
      public:
          MyList()
              : head(NULL)
          {
          }
      
          ~MyList()
          {
              delete_all();
          }
      
          MyNode* addToFront(float value)
          {
              head = new MyNode(value, head);
              return head;
          }
      
          /*
          MyNode* addToBack(float value)
          {
              MyNode **curr = &head;
              while (*curr != NULL)
                  curr = &((*curr)->next);
              return (*curr = new MyNode(value));
          }
          */
      
          void print() const
          {
              for(MyNode *curr = head; curr != NULL; curr = curr->next)
                  curr->print();
          }
      
          void delete_all()
          {
              for(MyNode *curr = head; curr != NULL; curr = head)
              {
                  head = curr->next;
                  delete curr;
              }
          }
      
          MyNode* search_val(float value)
          {
              for(MyNode *curr = head; curr != NULL; curr = curr->next)
              {
                  if (curr->data == value)
                      return curr;
              }
              return NULL;
          }
      };
      
      int main()
      {
          MyList ml;
      
          ml.addToFront(3.14f);
          ml.addToFront(2.99f);
          ml.addToFront(1.25f);
          ml.print();
      
          if (ml.search_val(3.14f) != NULL)
              std::cout << "found" << std::endl;
          else
              std::cout << "not found" << std::endl;
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 2015-02-21
        • 1970-01-01
        • 2012-08-18
        相关资源
        最近更新 更多