The code in Data book (5th Edition) from the 105 page to 106 page
Continuous updates
//Statement DataNode
typedef struct qnode
{
ElemType data;
struct qnode *next;
} DataNode;
//Statement LinkQuNode
typedef struct {
DataNode *front; //Point to single-list team head junction
DataNode *rear; //Point to single-list team end knot
} LinkQuNode;
//Initialize the queue
void InitQueue(LinkQuNode *&q) {
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
//Destroy the queue
void DestroyQueue(LinkQuNode *&q) {
DataNode *p=q->front, *r; //p point to team head data node
if (p!=NULL) { //Free data node footprint
r=p->next;
while (r!=NULL) {
free(p);
p=r; r=p->next;
}
}
free(p); free(q); //Release chain junction stakes in space
}
//Determine if the queue is empty
bool QueueEmpty(LinkQuNode *q) {
return(q->rear==NULL);
}
//Get in the team
void enQueue(LinkQuNode *&q, ElemType e) {
DataNode *p;
p=(DataNode *)malloc(sizeof(DataNode));
p->data=e;
p->next=NULL;
if (q->rear==NULL)
q->front=q->rear=p;
else {
q->rear->next=p; //Set resr point to node p
q->rear=p;
}
}
//Out of the team
bool deQueue(LinkQuNode *&q, ElemType &e) {
DataNode *t;
if (q->rear==NULL) return false; //When queue is null
t=q->front; //t point to first data node
if (q->front==q->rear)
q->front=q->rear=NULL;
else //When queue have multipe node
q->front=q->front->next;
e=t->data;
free(t);
return true;
}
如遇侵权,请联系作者删除