What’s wrong in my code I am trying to insert a node at
any given position in a doubly-linked list
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
node *prev;
};
class link{
private:
node *head;
node *tail;
public:
link()
{
head=NULL;
tail=NULL;
}
void insert(int pos, int value)
{
node *temp=new node;
node *pre=new node;
node *cur=new node;
if(head==NULL)
{
temp->next=NULL;
temp->prev=NULL;
temp->data=value;
head=temp;
}
else
{
if(pos==1 && head!=NULL)
{
temp->next=head;
temp->prev=NULL;
temp->data=value;
head=temp;
}
else
{
cur=head;
for(int i=1;i<pos;i++)
{
pre=cur;
cur=cur->next;
}
temp->data=value;
temp->next=cur->next;
temp->prev=cur;
cur->next=temp;
temp->next->prev=temp;
}
}
}
void display()
{
node *cur=new node;
cur=head;
while(cur!=NULL)
{
cout<<cur->prev<<" "<<cur->next<<" "<<cur->data<<endl;
cur=cur->next;
}
}
};
int main()
{
link obj;
//obj.display();
obj.insert(1,1);
obj.insert(2,2);
obj.insert(3,3);
obj.insert(4,4);
obj.insert(5,5);
obj.display();
return 0;
}