在列表末尾插入一个整数并在第 n 个位置删除

Inserting an integer at the end of the list and deleting at nth position

所以,在我的链表程序中,我想要它做的是询问用户要输入多少个数字,然后输入数字,并将这些数字添加到列表的末尾。然后,它将打印列表。之后,用户在列表中选择要删除的元素位置,然后重新打印列表。

#include <iostream>
using namespace std;

struct Node{

int data;
  Node* link;
};

Node* head;

void Insert(int data){ //insert an integer at the end of the list

  Node* temp = new Node();
  Node* temp2 = new Node();

  temp->data = data;
  temp->link = NULL;
  if(head = NULL){
    head = temp;
    return;
  }
  temp2 = head;
  while(temp2->link != NULL){
    temp2 = temp2->link;
  }

  temp2->link = temp;

}

void Delete(int n){ //delete an integer at nth position

  Node* temp1 = new Node();
  temp1 = head;
  if(n == 1){ //if the first node is to be deleted
    head = temp1->link; //now head points to second node
    delete temp1; //delete first node
    return;
  }

  for(int i = 0; i < n-2; i++){
    temp1 = temp1->link; //temp1 points to (n-1)th node
  }
  Node* temp2 = temp1->link; //temp2 points to nth node
  temp1->link = temp2->link; // pointing to (n+1)th node
  delete temp2; //deleting nth node

}

void Print(){ //print out the list
  Node* printNode = head;
  cout << "List: ";

  while(printNode != NULL){
    cout << printNode->data;
    cout << " ";
    printNode = printNode->link;
  }

  cout << "\n";
}

int main(){

  int x, count, n;

  head = NULL; //start with an empty list

  cout << "How many numbers? " << endl;
  cin >> count;

  for(int i = 0; i < count; i++){

    cout << "Enter number: ";
    cin >> x;
    Insert(x);
  }

  Print();

  cout << "Enter position to delete: ";
  cin >> n;

  Delete(n);
  Print();

  return 0;

}

接受第一个数字后,程序停止运行。我能知道我在哪里做错了代码吗?我该怎么做才能使这段代码更有效率?提前致谢。

您可能需要重新考虑您的插入功能。您的代码崩溃的部分是在 while 循环插入期间。如果您希望 temp2 保存数据,那么您需要为其动态分配 space 。但是,您只是将它用作位置指示器(遍历列表) - 那么为什么您需要分配 space 来指向列表中的头部或任何其他节点位置?

下面是我将如何插入列表(当然是在后面):

void Insert(int data){ //insert an integer at the end of the list

    Node* temp = new Node();

    // This is to ensure that temp was created -> Also called defensive programming.
    if (!temp)
   {
        cout << "We did not have enough space alloted to dynamically allocate a node!" << endl;
        exit(1);
    }

    temp->data = data; // Bad nominclature for program; Don't use the same name twice.
    temp->link = NULL;

    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        // This is to help traverse the linked list without having to
        // manipulate the position of what head points to.
        Node *Pos_Indicator = head;

        while (Pos_Indicator->link != NULL)
        {
            Pos_Indicator = Pos_Indicator->link;
        }

        // We are at the end of the list, it is now safe to add.
        Pos_Indicator->link = temp;

        // Should probably have a check here for whether it was successful or not.
    }

}

我能够编译并 运行 你的代码完成,没有其他问题。让我知道这是否有帮助!

编辑:或者您知道 (head = NULL)(head == NULL) 也有效 :(

我的脸大了,只是一个小错误。代码已更正。

if(head == NULL){
    head = temp;
    return;
}