大家好,我已经编写了双向链表程序,没有错误,但是当我插入并显示列表时,列表为空,以下是我的显示代码和 addatbeg 函数
void addatbeg()// Ins`erting element in the beg
{
struct dsnode*r,*q=p;
if(p==NULL) //If the list is empty
{
//creating a new node
p=(struct dsnode*)malloc(sizeof(struct dsnode));
p->prev=NULL;
p->data=item;
p->next=NULL;
}
else
{
while(q->next!=NULL)//Traverse the linked list till the last node is reached
{
q=q->next;
}
//add a new node in the end
r=(struct dsnode*)malloc(sizeof(struct dsnode));
r->prev=q;
r->data=item;
r->next=NULL;
q->next=r;
}
}
void dis() //Display
{
while(p!=0)
{
printf("%2d ",p->data);
p=p->next;
}
}