简体   繁体   English

在双链表上插入函数

[英]Insert Function on Doubly Linked List

So I've got this insert function for a doubly linked list that is working for the most part just up until I try to insert a new node at a given index. 因此,我为双向链接列表提供了这个插入功能,该列表在大多数情况下一直有效,直到我尝试在给定索引处插入新节点。 I'm having trouble with linking it correctly to the nodes before and after it, if anyone could see why, I keep getting errors when I try to assign one of the points I'll point out in the code: 我在将它正确地链接到它之前和之后的节点时遇到了麻烦,如果有人能看到原因,当我尝试分配代码中要指出的要点之一时,我总是会出错:

  void insert(int index, ItemType& item) 
  {

    int pos = 0;
    Node* current = new Node;
    Node* n = new Node;
    n->info = item;
    if (index >= 0 && index <= size)
    {
        if (size == 0)
        {
            head = n;
            tail = n;
            n->next = NULL;
            n->prev = NULL;
            size++;
            return;
        }
        else if (index == 0)
        {
            n->prev = NULL;
            n->next = head;
            head->prev = n;
            head = n;
            size++;
            return;
        }
        else if (index == size)
        {
            n->next = NULL;
            n->prev = tail;
            tail->next = n;
            tail = n;
            size++;
            return;
        }
        else if (index < size/2)
        {
            current = head;
            while(pos != index)
            {
            current = current->next;
            pos++;
            }

        }
        else if (index > size/2)
        {
            int endpos = size - 1;
            current = tail;
            while(endpos != index)
            {
            current = current->prev;
            endpos--;

            }
        }


    n->next = current;
    current->prev->next = n; // HERE is where the code breaks, don't know why.
    n->prev = current->prev;
    current->prev = n;
    size++;


  }
}

So the code breaks at the current->prev->next = n statement stating there is an access violation writing location. 因此,代码在current-> prev-> next = n语句处中断,指出存在访问冲突写入位置。 So I'm not sure if that is coded right or if I've messed up in pointing assignments in earlier code. 所以我不确定这是正确的编码还是在之前的代码中弄乱了指向分配。 If anyone knows why its doing that and can point me in the right direction that would be awesome. 如果有人知道为什么这样做,并且可以指出正确的方向,那将是很棒的。 Thanks. 谢谢。

From my observation, 根据我的观察,

  1. Your code fails when index = size/2 . index = size/2时,您的代码失败。

When there are two elements(size == 2) and when you try to insert at position 1, then current->prev->next = n; 当有两个元素(大小== 2)并且尝试在位置1插入时,那么current->prev->next = n; is meaningless 没有意义

Do one of these changes else if (index <= size/2) or else if (index >= size/2) else if (index <= size/2)else if (index >= size/2) else if (index <= size/2)执行这些更改之一

If current is the first node in the list, then current->prev will be NULL , so current->prev->next will cause problems. 如果current是列表中的第一个节点,那么current->prev将为NULL ,因此current->prev->next将引起问题。 You should check if current is the first item in the list before this line. 您应该检查current是否是该行之前列表中的第一项。

Also, your code leaks memory because you are allocating a new Node for current and you do not delete it. 同样,您的代码会泄漏内存,因为您正在为current分配一个new Node ,并且没有删除它。 Since you are using current to move through the list rather than to create a new node, you should declare it as just 由于您正在使用current在列表中移动而不是创建新节点,因此应将其声明为

Node* current;

rather than 而不是

Node* current = new Node;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM