You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双向循环链表中间插入节点异常问题求助

Fixing Your Circular Doubly Linked List Issues

Let's walk through the problems in your code one by one, fix them, and get you the expected output.

1. Wrong Parameter in addin Call

In your main function, after reading pos and val1, you called l1.addin(val, pos); — but val holds the last value from your initial list (3 in your input), not the new value 0 you want to insert. This is the first reason your output is wrong.

Fix that line to:

l1.addin(val1, pos);

2. Broken Display Function (disp)

Your original disp logic doesn't properly traverse a circular linked list. The while(temp->next != head) condition stops before the tail node, then you jump back to the head and print it again — which is why your initial output might have been incorrect, and the post-insertion output is broken.

Use a do-while loop instead (we need to print the head node first before checking if we've looped back):

void disp() //display function
{
    if (head == nullptr) { // Handle empty list to avoid crashes
        return;
    }
    node *temp = head;
    do {
        cout << temp->data << " ";
        temp = temp->next;
    } while (temp != head);
    cout << endl;
}

3. Missing Tail Update in addin (Edge Case)

Your addin works for middle insertions, but if you insert at the end of the list, you need to update the tail pointer to point to the new node. Otherwise, the circular structure will be inconsistent for future operations.

Add this check at the end of the addin function:

if (temp == tail) {
    tail = newnode;
}

Fixed Full Code

Here's the complete corrected code:

#include<iostream>
using namespace std;
class node //node class
{
public:
    int data;
    node *next;
    node *prev;
    node(int a)
    {
        data=a;
        next=nullptr;
        prev=nullptr;
    }
};
class linkedlist //linkedlist class
{
    node *head,*tail;
public:
    linkedlist()
    {
        head=nullptr;
        tail=nullptr;
    }
    void addnode(int val) //creating node function
    {
        node *newnode;
        newnode=new node(val);
        if(head==nullptr)
            head=tail=newnode;
        else
        {
            tail->next=newnode;
            head->prev=newnode;
            newnode->prev=tail;
            newnode->next=head;
            tail=newnode;
        }
    }
    void disp() //display function
    {
        if (head == nullptr) {
            return;
        }
        node *temp=head;
        do {
            cout<<temp->data<<" ";
            temp=temp->next;
        } while(temp != head);
        cout<<endl;
    }
    void addin(int val,int pos) //addin function
    {
        node *newnode=new node(val);
        node *temp=head;
        for(int i=0;i<pos;i++)
        {
            temp=temp->next;
        }
        newnode->next=temp->next;
        newnode->prev=temp;
        temp->next->prev=newnode;
        temp->next=newnode;
        
        // Update tail if inserting at the end of the list
        if (temp == tail) {
            tail = newnode;
        }
    }
};
int main()
{
    linkedlist l1;
    int s,val,val1,pos;
    cin>>s;
    for(int i=0;i<s;i++)
    {
        cin>>val;
        l1.addnode(val);
    }
    l1.disp(); //display function
    cout<<"\n\n";
    cin>>pos;
    cin>>val1;
    l1.addin(val1,pos); //calling addin function
    l1.disp();
}

Troubleshooting & Programming Tips for Beginners

  • Print Debug Info: When your output is wrong, add cout statements inside functions (like addin) to print node values and pointer connections. This helps you see exactly where the chain breaks.
  • Test Tiny Cases: Start with 1 or 2 nodes instead of 3. It's much easier to trace through each step manually.
  • Draw the List: Sketch nodes and pointers on paper before writing code. For insertions/deletions, map out how each pointer should change — this avoids silly logic errors.
  • Handle Edge Cases: Always account for empty lists, insertions at the start/end, or invalid positions (you could add a check in addin to make sure pos is within bounds).
  • Use Clear Variable Names: Instead of val and val1, use names like new_value or initial_list_value — this reduces mistakes when passing parameters.

内容的提问来源于stack exchange,提问作者BATMAN_X

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:32:57