簡體   English   中英

當我試圖在雙循環鏈表之間輸入一個節點時,它給出了錯誤的 output

[英]When I am trying to enter a node between doubly circular linked list it gives wrong output

在這里,我創建了一個雙循環鏈表並嘗試在列表之間添加一個節點,但它給出了錯誤的 output

我首先創建了一個雙循環鏈表,然后顯示它,然后我在它們之間添加了一個節點,然后再次顯示它

#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==0)
            head=tail=newnode;
        else
        {
            tail->next=newnode;
            head->prev=newnode;
            newnode->prev=tail;
            newnode->next=head;
            tail=newnode;
        }
    }
    void disp()                             //display function
    {
        node *temp=head;
        while(temp->next!=head)
        {
            cout<<temp->data<<"   ";
            temp=temp->next;
        }
        temp=temp->next;
        cout<<"   "<<temp->data;
    }
    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;
    }
};
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(val,pos);                      //calling addin function
    l1.disp();
}

輸入

3

1 2 3

1

0

預期 output

1  2  3

1 2 0 3

當前 output

1  2  3   1

我不知道我在做什么錯誤,而且我是初學者,所以任何提示對我來說也太有幫助了

  • 由於額外的temp=temp->next;您正在打印頭節點而不是尾節點disp() function 中的循環之后。
  • 您在main() function 中的循環之后添加val而不是val1
    void disp()                             //display function
    {
        node *temp=head;
        while(temp->next!=head)
        {
            cout<<temp->data<<"   ";
            temp=temp->next;
        }
        // remove this
        // temp=temp->next;
        cout<<"   "<<temp->data;
    }
    cin>>pos;
    cin>>val1;
    // add val1, not val
    //l1.addin(val,pos);                      //calling addin function
    l1.addin(val1,pos);                      //calling addin function
    l1.disp();

在主要 function 你錯誤地添加 val 而不是 val1

cin>>val1;
l1.addin(val1,pos); 

然后在 disp() function 中,刪除循環外的 temp=temp->next。

void disp()                             //display function
    {
        node *temp=head;
        while(temp->next!=head)
        {
            cout<<temp->data<<"  ";
            temp=temp->next;
        }
        // temp=temp->next;
        cout<<"  "<<temp->data;
    }

最后在 addin() function 中,將 pos 更改為 pos-1

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

就這樣。

@MikeCAT 是正確的,您正在使用 val1 並傳遞 val。 但是,我認為 disp 中的 while 循環將跳過最后一個元素而不打印它。 並且插件中的邏輯不會在新位置添加新元素。 這是我的代碼版本

#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 == NULL)
            head = tail = newnode;
        else
        {
            tail->next = newnode;
            head->prev=newnode;
            newnode->prev=tail;
            newnode->next=head;
            tail=newnode;
        }
    }
    void disp()                             //display function
    {
        node *temp = head;
        if (temp == NULL) 
        {
            cout << "List is empty " << endl;
            return;
        }
        do
        {
            cout << temp->data << endl;
            temp = temp->next;
        } while (temp != head);

    }
    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->next=temp;
        newnode->prev=temp->prev;
        temp = temp->prev;
        temp->next = newnode;
    }
};
int main()
{
    linkedlist l1;
    int s,val,pos;
    cin>>s;
    cout << "val of size is " << s << endl;
    for(int i=0;i<s;i++)
    {
        cin>>val;
        l1.addnode(val);
    }
    l1.disp();                              //display function
    cout<<"\n\n";
    cin>>pos;
    cin>>val;
    l1.addin(val,pos);                      //calling addin function
    l1.disp();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM