我是链表的新手。每次我编写代码时都会出现运行时错误。与此相同,我在该程序中也遇到运行时错误。请解释代码中的错误。我试图找出错误,但对我来说一切似乎都很好。请解释。
# include <iostream>
using namespace std;
struct node
{
int a;
struct node *next;
};
typedef struct node node;
node *front = NULL;
node *rear = NULL;
void enqu(int b)
{
node *p;
p->a = b;
if(front == NULL)
{
p->next = NULL;
front = p;
rear = p;
}
else
{
p->next = NULL;
rear->next = p;
rear = p;
}
}
void dequ()
{
node *p;
if (front != NULL)
{
if(front == rear)
{
front = NULL;
rear = NULL;
}
else
{
front=front->next;
}
cout<<"no deleated is"<<p->a<<"\n";
}
else
{
cout<<"queue is empty";
}
}
void display()
{
node *p;
if(p!=NULL)
{
p=front;
while(p!=NULL)
{
cout<<p->a;
p=p->next;
}
}
else
{
cout<<"queue is empty";
}
}
int main()
{
enqu(1);
enqu(2);
enqu(5);
enqu(6);
enqu(7);
enqu(8);
display();
dequ();
dequ();
display();
return 0;
}