我的代码无法正常工作。谁能指出错误?

My code isn't working properly as it should be. Can anyone point out the errors?

我正在练习双向链表中的插入。我不知道为什么这段代码不能正常工作。此代码的输出是:

position doesn't exist

position doesn't exist

1
2

如您所见,它甚至没有打印第三个数据。我知道我做错了什么,但我无法识别这段代码中的错误。请帮帮我。

#include <iostream>

using namespace std;

struct dllnode
{
    int data;
    dllnode *next,*prev;
};
class dlinked_list
{
    dllnode *head,*tail;
public:
    dlinked_list()          //Constructor
    {
        head=NULL;      
        tail=NULL;
    }
    void insertionindll(int ,int);
    static void display(dllnode *);
    dllnode* gethead()
    {
        return head;        //returning head pointer of the linkedlist
    }
    dllnode* create_node(int data);
};
dllnode* dlinked_list::create_node(int n)   //Creating a new node
{
    dllnode *temp=new dllnode;
    temp->data=n;
    temp->prev=NULL;
    temp->next=NULL;
    return temp;    //returning the address of the newly created node to line no. 39
}
void dlinked_list::insertionindll(int n, int pos)
{
    dllnode *temp;
    int k=1;
    dllnode *newnode= create_node(n);   //storing address of the newly created node
    if(!newnode)
    {
        cout<<"Memory Error";   //Checking memory error
        return;
    }
    newnode->data=n;
    if(pos==1)  //Insertion at the beginning//
    {
        newnode->next=head;     
        newnode->prev=NULL;
        if(head)        //checking if head!=NULL
            head->prev=newnode;
        head=newnode;   //now head points to the newly created node
        return;
    }
    temp=head;      //temp is now pointing to the first node
    //After this loop temp will either point to the last node 
    //or the previous node at which we want to insert newnode
    while(k<pos && temp->next!=NULL)    
    {
        k++;    //incrementing k
        temp=temp->next;    
    }
    if(k!=pos)  //checking if the position doesn't exist
    {
        cout<<"position doesn't exist"<<endl;
    }
    newnode->prev=temp;     
    newnode->next=temp->next;
    if(temp->next)
        temp->next->prev=newnode;
    temp->next=newnode;
    return;
}
void dlinked_list::display(dllnode *a)  //for displaying the created linked list
{
    dllnode *ptr;   //declaring a pointer which points to the head of the linked list
    ptr=a;
    while(ptr->next!=NULL)  //iterating untill the last node
    {
        cout<<ptr->data<<endl;  //printing data of the linked list
        ptr=ptr->next;  //pointng to the next node 
    }
    return;
}
int main()
{
    /* code */
    dlinked_list a; //creating an object a of class dlinked_list
    a.insertionindll(1,1);  //here insertionindll function is called 
    a.insertionindll(2,2);
    a.insertionindll(3,3);
    dlinked_list::display(a.gethead()); //display function is called by passing head pointer
    return 0;
}

您还没有添加第三个节点。根据你的代码,要添加第三个节点,你的温度应该停在第二个节点,k 将为 2.

检查一下:

 if(k!=pos-1)  //checking if the position doesn't exist

打印数据时也有一个空指针检查:

while(ptr!=null && ptr->next!=NULL)  //iterating untill the last node