C 链接的列表用户输入限制验证导致程序结束



我有一个基于位置函数的删除节点,并且我试图使用用户输入大于链接列表大小或IN时用户输入会重复自身的逻辑进行验证其他单词不限制。

i声明了int size,并且每当创建一个新节点时,它都会增加自身,反之亦然。这是我的删除节点函数:

    void delete_position(int pos) //delete node with given position
    {
        node *current=new node;
        node *previous=new node;
        current=head;
        if(head==NULL)
        {
            cout<<"You haven't ordered any cake(s)"<<endl;
        }
        for(int i=1;i<pos;i++)
        {
            previous=current;
            current=current->next;
        }
        previous->next=current->next;
        if(pos == 1){
            node *temp=new node;
            temp=head;
            head=head->next;
            delete temp;
        }
        size--;
    }

这是我的验证,我使用do while loop来检查用户的输入,因此,只要输入大于大小,它将再次循环用户输入。问题是,说我的列表中只有2个节点,我输入4,程序跳到下一行并结束。

cout<<"====destroy node===="<<endl;
cout<<"Which cake would you like to destroy?"<<endl;
do{
cin>>destroy_input;
}while(destroy_input > size);
list.delete_position(destroy_input);

这是我在pastebin中的代码:https://pastebin.com/vfurymlk

重新填写我的代码:对我有用,如果缺少或错误,请随时编辑,我已经摆脱了此功能的内存泄漏,对此主题很新,对不起,我以前的错误。

void delete_position(int pos) //delete node with given position
        {
            node *current = head;
            if(size == 1){
                head=NULL;
                tail=NULL;
            }
            else{
                if(pos == 1){
                    head = head->next;
                }else{
                    node *previous = NULL;
                    for(int i=1; i<pos; i++){
                        previous = current;
                        current = current->next;
                    }
                    if(current == tail){
                        previous->next =NULL;
                        tail = previous;
                    }else{
                        previous->next = current->next;
                    }
                }
                delete current;
                size--;
            }
        }

您的功能中有一些错误。

void delete_position(int pos) //delete node with given position
{
  node *current=new node;  // New? Why?
  node *previous=new node; // New? Why?
  current=head; // current was not deleted. Memory leak.

为什么在这里创建两个节点?您将在此功能中失去指针的第一件事,因此您肯定会有内存泄漏,然后进入for后立即丢失previous。您正在泄漏两个node

for(int i=1;i<pos;i++) // start at position 1, since you already have head.
{
  previous = current;
  current = current->next;
}
previous->next = current->next;

请注意,如果删除节点的位置为1,则切勿输入for循环。但是,您仍然将previous->next分配给current->next。在您的情况下,以前的设置为已将其初始化为默认值的新节点。简而言之,列表中不在列表中的新节点获得head->next的值。

if(pos == 1)
{
  node *temp = new node; // create a new node again? Why?
  temp = head; // You lost your newly created memory. New memory leak here.
  head = head->next;
  delete temp;
}
size--;

如果条件通过,则创建一个新的默认节点。您迅速失去了用head的指针值覆盖它的指针。然后它成为第二个节点并被删除。

您的列表是基于1的吗?看来您试图在pos == 1时为head提供特殊情况。

如果应该是基于0的,则更好的解决方案是:

bool delete_position(int pos) //delete node with given position
{
  node *current = head;
  node *previous = nullptr;
  bool deleted = false;
  // If your list is 1-based, though I don't see why it would be, uncomment the following line.
  // pos--;
  if((size > 0) && (pos < size))
  {
    // Search fo the node
    for(int i = 1; i < pos; i++)
    {
      previous = current;
      current = current->next;
    }
    // Connect next node with previous node (if it's not head)
    if(previous)
    {
      previous->next = current->next;
    }
    // Remove the current node
    delete current;
    size--;
    deleted = true;
  }
  return deleted;
}

我尚未分析您的所有程序,但我怀疑您可能还会有其他一些内存泄漏。查找没有相应deletenew

最新更新