C++ 我的代码不是';它不能正常工作。有人能指出错误吗?

C++ 我的代码不是';它不能正常工作。有人能指出错误吗?,c++,doubly-linked-list,C++,Doubly Linked List,我正在练习在双链表中插入。我不知道为什么这个代码不能正常工作。此代码的输出为: position doesn't exist position doesn't exist 1 2 如您所见,它甚至没有打印第三个数据。我知道我做错了什么,但我无法识别代码中的错误。请帮帮我 #include <iostream> using namespace std; struct dllnode { int data; dllnode *next,*prev; }; cla

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

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;
}
#包括
使用名称空间std;
结构dllnode
{
int数据;
dllnode*next,*prev;
};
类链接列表
{
dllnode*头,*尾;
公众:
dlinked_list()//构造函数
{
head=NULL;
tail=NULL;
}
void insertionindll(int,int);
静态空洞显示(dllnode*);
dllnode*gethead()
{
return head;//返回linkedlist的head指针
}
dllnode*创建_节点(int数据);
};
dllnode*dlinked_list::create_node(int n)//创建新节点
{
dllnode*temp=新的dllnode;
温度->数据=n;
temp->prev=NULL;
temp->next=NULL;
return temp;//将新创建的节点的地址返回到第39行
}
void dlinked_list::insertionindll(int n,int pos)
{
dllnode*temp;
int k=1;
dllnode*newnode=create_node(n);//存储新创建节点的地址
如果(!newnode)
{
coutnext=头部;
newnode->prev=NULL;
if(head)//检查head!=NULL
head->prev=newnode;
head=newnode;//现在head指向新创建的节点
返回;
}
temp=head;//temp现在指向第一个节点
//在此循环之后,temp将指向最后一个节点
//或者要插入新节点的上一个节点
while(knext!=NULL)
{
k++;//增加k
温度=温度->下一步;
}
if(k!=pos)//检查位置是否不存在
{
coutnext)
temp->next->prev=newnode;
temp->next=newnode;
返回;
}
void dlinked_list::display(dllnode*a)//用于显示创建的链表
{
dllnode*ptr;//声明指向链接列表头部的指针
ptr=a;
while(ptr->next!=NULL)//迭代到最后一个节点
{

cout您尚未添加第三个节点。根据您的代码,要添加第三个节点,您的temp应该在第二个节点停止,k将为2

请出示这张支票:

 if(k!=pos-1)  //checking if the position doesn't exist
在打印数据时,还要进行空指针检查:

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

当您使用调试器一次执行程序时,在使用调试器检查所有变量的值后,在执行程序的每一行之后,您提出了什么观察?建议:而不是
create棼node
方法,使
dllnode
constructor更智能。例如:
dllnode(int indata):数据(indata)、上一个(NULL)、下一个(NULL){}
。另一个建议是:
if(!newnode)
在创建带有
new
的节点后不需要。
new
在失败时抛出异常,保证您永远不会到达这里(除非你改变了
new
的行为,这是一个高级的话题,你暂时不应该搞砸)。仔细想想为什么
while(ptr->next!=NULL)
测试
ptr->next
而不首先确保
ptr
不是
NULL
。可能重复感谢您对@user4581301内存检查的第二次建议,是的,显示函数的while循环出现问题。应该是:while(ptr!=NULL)但我不明白你关于智能构造函数的观点。我认为只有在我们创建任何对象时才会调用构造函数。在我的例子中,我正在处理一个列表,所以如果我删除create_node方法,这对创建新节点有何帮助?我的意思是,每次需要从主函数创建新节点时,我如何调用构造函数?