无法将项目插入链接列表中

发布于 2025-01-23 02:26:30 字数 900 浏览 3 评论 0原文

void LinkedList::insert(int num, int pos) {
  Node *newNode = new Node;
  newNode->data = num;
  newNode->next = NULL;
  if(pos == 0) {
    newNode->next = head;
    head = newNode;
  }
  else {
    Node *temp = head;
    for (int i = 1; i < pos-1; ++i) {
      if (temp != NULL) {
        temp = temp->next;
      }
    }
    if (temp != NULL) {
      newNode->next = temp->next;
      temp->next = newNode;
    }
    else {
      cout << " The previous node is full.";
    }
  }
}

这是我的插入功能。 在main.cpp中运行的代码是:

// adding through insert
    nums.insert(1, 0);
    nums.insert(5, 4);
    nums.insert(3, 7);

输出是:

List after append: 
8   6   7   8   0   9

List after inserting: 
1   8   6   5   7   8

如您所见,某些东西被覆盖了,或者列表的结尾被切断了。我已经搜索了互联网几个小时,无济于事。输出需要增加列表的长度而不是覆盖任何内容。任何帮助将不胜感激。

void LinkedList::insert(int num, int pos) {
  Node *newNode = new Node;
  newNode->data = num;
  newNode->next = NULL;
  if(pos == 0) {
    newNode->next = head;
    head = newNode;
  }
  else {
    Node *temp = head;
    for (int i = 1; i < pos-1; ++i) {
      if (temp != NULL) {
        temp = temp->next;
      }
    }
    if (temp != NULL) {
      newNode->next = temp->next;
      temp->next = newNode;
    }
    else {
      cout << " The previous node is full.";
    }
  }
}

This is my insert function.
The code that runs in main.cpp is:

// adding through insert
    nums.insert(1, 0);
    nums.insert(5, 4);
    nums.insert(3, 7);

And the output is:

List after append: 
8   6   7   8   0   9

List after inserting: 
1   8   6   5   7   8

As you can see, something is getting overwritten, or the end of the list just gets cut off. I have searched the internet for hours to no avail. The output needs to increase the length of the list and not overwrite anything. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-01-30 02:26:30

问题在于,对于循环,

for (int i = 1; i < pos-1; ++i) {
  if (temp != NULL) {
    temp = temp->next;
  }
}

让我们假设pos等于2。在这种情况下,循环不会迭代,temp等于head。因此,将在第二个模式之前插入新节点,而在第三个节点之前插入。

您还需要编写

for (int i = 1; temp != nullptr && i < pos; ++i) {
    temp = temp->next;
}

而不是编写此消息

 cout << " The previous node is full.";

(该函数的呼叫者应该决定是否输出消息),我将声明函数喜欢

bool LinkedList::insert(int num, int pos) {

并返回对是否插入节点的依赖。

The problem is this for loop

for (int i = 1; i < pos-1; ++i) {
  if (temp != NULL) {
    temp = temp->next;
  }
}

Let's assume that pos is equal to 2. In this case the loop will not iterate and temp will be equal to head. So the new node will be inserted before the second mode instead to be inserted before the third node.

You need to write

for (int i = 1; temp != nullptr && i < pos; ++i) {
    temp = temp->next;
}

Also instead of writing this message

 cout << " The previous node is full.";

(it is the caller of the function should decide whether to output a message) I would declare the function like

bool LinkedList::insert(int num, int pos) {

and return either true or false dependent on whether a node was inserted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文