//创建一个带头结点的空队: |
LQueue *Init_LQueue() |
{ |
LQueue *q,*p; |
q= malloc ( sizeof ( LQueue ) ); /*申请头尾指针结点*/ |
p= malloc ( sizeof ( QNode ) ); /*申请链队头结点*/ |
p->next=NULL; |
q->front=q->rear=p; |
return q; |
} |
// 入队 |
void In_LQueue ( LQueue *q , datatype x ) |
{ |
QNode *p; |
p= malloc ( sizeof ( QNnode ) ); /*申请新结点*/ |
p->data=x; |
p->next=NULL; |
q->rear->next=p; |
q->rear=p; |
} |
//判队空 |
int Empty_LQueue ( LQueue *q ) |
{ |
if ( q->front==q->rear ) return 0; |
else return 1; |
} |
//出队 |
int Out_LQueue ( LQueue *q , datatype *x ) |
{ |
QNnode *p; |
if ( Empty_LQueue ( q ) ) |
{ |
printf ( "队空" ) ; return 0; |
} /*队空,出队失败*/ |
else |
{ |
p=q->front->neat; |
q->front->next=p->next; |
*x=p->data; /*队头元素放x 中*/ |
free ( p ); |
if ( q->front->next==NULL ) |
q->rear=q->front; |
/*只有一个元素时,出队后队空,此时还要要修改队尾指针参考图3.16(c)*/ |
return 1; |
} |
} |