指针和静态和新对象

bre*_*ttb 1 c++ linked-list

首先,如果这是一个重复的问题,请道歉.我只是在学习C++,可能不知道正确的搜索术语来找到我确定已经被问到的内容.

无论如何,我通过在HackerRank完成 30天的代码来教自己C++ .但是,当我被要求insert为LinkedList 实现一个方法时,我遇到了一个似乎无法解决的障碍.从概念上讲,我知道需要做什么,但从语法上讲,我遇到了一个问题.

下面是我的代码,包括调试打印输出.似乎正在发生的事情是new_node不断地将它放在内存中的相同位置,而不管它在哪个循环迭代中.我如何确保在内存中获得新的位置?无论我是否声明new_node,我似乎都会得到相同的行为static.

这是代码:

#include <iostream>
#include <cstddef>
using namespace std;    
class Node
{
    public:
        int data;
        Node *next;
        Node(int d){
            data=d;
            next=NULL;
        }
};
class Solution{
    public:
    /// ----- MY CODE BEGINS HERE:
      Node* insert(Node *head,int data)
      {
          cout << "----------" << endl;
          cout << data << endl;
          int i = 0;

          if (head){
              Node *curr = head;
              Node *next = curr->next;

              while(next){
                  cout << data << "," << i << ": " << curr << "," << curr->next 
                   << "," << curr->data << endl;
                  i++;
                  curr = curr->next;
                  next = curr->next;
              }
              cout << data << "," << i << ": " << curr << "," << curr->next 
                   << "," << curr->data << endl;
              static Node new_node = Node(data);
              curr->next = &new_node;
              cout << " ***  Adding " << data << " at " << curr->next
                   << " and next points to: " << (curr->next)->next << endl;
              return head;
          }
          else{
              static Node new_head = Node(data);
              cout << " ***  Adding " << data << " at " << &new_head
                   << " and next points to: " << new_head.next << endl;
              return &new_head;
          }
      }
      // ------- MY CODE ENDS HERE
      void display(Node *head)
      {
          Node *start=head;
          while(start)
          {
              cout<<start->data<<" ";
              start=start->next;
          }
      }
};
int main()
{
    Node* head=NULL;
    Solution mylist;
    int T,data;
    cin>>T;
    while(T-->0){
        cin>>data;
        head=mylist.insert(head,data);
    }   
    mylist.display(head);

}
Run Code Online (Sandbox Code Playgroud)

当我使用(4,2,3,4,1)的示例输入运行时,我得到以下内容:

----------
2
 ***  Adding 2 at 0x6022e0 and next points to: 0
----------
3
3,0: 0x6022e0,0,2
 ***  Adding 3 at 0x7fff3ddc1d80 and next points to: 0
----------
4
4,0: 0x6022e0,0x7fff3ddc1d80,2
4,1: 0x7fff3ddc1d80,0,3
 ***  Adding 4 at 0x7fff3ddc1d80 and next points to: 0x7fff3ddc1d80
----------
1
1,0: 0x6022e0,0x7fff3ddc1d80,2
1,1: 0x7fff3ddc1d80,0x7fff3ddc1d80,4
1,2: 0x7fff3ddc1d80,0x7fff3ddc1d80,4
1,3: 0x7fff3ddc1d80,0x7fff3ddc1d80,4
1,4: 0x7fff3ddc1d80,0x7fff3ddc1d80,4
1,5: 0x7fff3ddc1d80,0x7fff3ddc1d80,4
Run Code Online (Sandbox Code Playgroud)

并且这一直持续到分段错误因为它陷入无限循环...

任何想法为什么new_node一直放在相同的内存位置(有或没有static)?这甚至不是主要问题,我完全忽略了这一点?提前致谢!

- C++新手.


编辑:建议的副本不完全解决这里的问题.我的麻烦是没有理解指针和引用之间的区别,而是区别:

Node node_1 = Node(data);
static node_2 = Node(data);
node_3 = new Node(data);
Run Code Online (Sandbox Code Playgroud)

因为我new在撰写问题时并不知道操作员(显然!),我不知道(a)搜索这个或(b)在标题中包含这个术语.为了清晰起见,标题已经过编辑,此编辑已包含在未来的读者中.

Bar*_*mar 5

声明变量时static,该变量只有一个副本.它是在您第一次执行声明时创建的,并且将来对函数的调用将重用相同的数据.因此,每次使用时new_node,它都是相同的节点.

您需要与new操作员分配动态数据.正如运算符名称所暗示的那样,每次使用它时都会创建一个新对象.remove()向类中添加操作时,它将delete用于释放内存.

  Node* insert(Node *head,int data)
  {
      cout << "----------" << endl;
      cout << data << endl;
      int i = 0;

      if (head){
          Node *curr = head;
          Node *next = curr->next;

          while(next){
              cout << data << "," << i << ": " << curr << "," << curr->next 
               << "," << curr->data << endl;
              i++;
              curr = curr->next;
              next = curr->next;
          }
          cout << data << "," << i << ": " << curr << "," << curr->next 
               << "," << curr->data << endl;
          Node *new_node = new Node(data);
          curr->next = new_node;
          cout << " ***  Adding " << data << " at " << curr->next
               << " and next points to: " << (curr->next)->next << endl;
          return head;
      }
      else{
          Node *new_head = new Node(data);
          cout << " ***  Adding " << data << " at " << &new_head
               << " and next points to: " << new_head->next << endl;
          return new_head;
      }
  }
Run Code Online (Sandbox Code Playgroud)