队列常见的基本操作
InitQueue(&Q)
QueueEmpty(Q)
EnQueue(&Q,x)
DeQueue(&Q,&x)
GetHead(Q,&x)
队列的链式存储类型和描述为
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
- 初始化
void InitQueue(LinkQueue &Q) {
Q.front == Q.rear = (LinkNode*) malloc (sizeof(linkNode));
Q.front-> next = NULL;
}
- 判队空
bool IsEmpty (LinkQueue Q){
if(Q.front == Q.rear) return true;
else return false;
}
- 入队
void EnQueue(LinkNode &Q, ElemType x){
LinkNode *s = (linkNode *)malloc(sizeof(LinkNode));
s->data = x; s->next = NULL;
Q.rear->next = s;
Q.rear = s;
}
- 出队
bool DeQueue(LinkNode &Q, ElemType &x){
if(Q.rear == Q.front) return false;
LinkNode &p = Q.front->next;
x = p->data;
Q.front -> next = p->next;
if(Q.rear==p)
Q.rear = Q.front;
free(p);
return true;
}