1. 基本操作
操作 | 解释 |
---|---|
MakeNull(Q) | 将队列置空 |
Front(Q) | 返回队列第一个元素 |
EnQueue(x,Q) | 将元素插入Q的后端 |
DeQueue(Q) | 删除第一个元素 |
Empty(Q) | 为空返回TRUE |
2. 队列的指针实现
struct celltype { Elementtype element; celltype *next; }; struct QUEUE { celltype *front; celltype *rear; }; void MakeNull(QUEUE &Q) { Q.front = new celltype; Q.front->next = NULL; Q.rear = Q.front; } boolean Empty(QUEUE Q) { if(Q.front == Q.rear) return TRUE; else return FALSE; } void EnQueue(Elementtype x, QUEUE &Q) { Q.rear->next = new celltype; Q.rear = Q.rear->next; Q.rear->element = x; Q.rear->next = NULL; } void DeQueue(QUEUE &Q) { celltype *tmp; if(Empty(Q)) error("empty"); else { tmp = Q.front->next; Q.front->next = tmp->next; delete tmp; if(Q.front->next == NULL) Q.rear = Q.front; } }
3. 队列的数组实现
#define maxlength 100 typedef struct { int front; int rear; Elementtype elements[maxlength]; } QUEUE; int addone(int i ) { return (i+1)%maxlength; } void MakeNull(QUEUE &Q) { Q.front = 0; Q.rear = maxlength - 1; } boolean Empty(QUEUE Q) { if(addone(Q.rear) == Q.front) return TRUE; else return FALSE; } Elementtype Front(QUEUE Q) { if(Empty(Q)) return NULL; else return Q.elemenmts[Q.front]; } void EnQueue(elementtype x, QUEUE &Q) { if(addone(addone(Q.rear)) == Q.front) error("queue is full"); else { Q.rear = addone(Q.rear); Q.elements[Q.rear] = x; } } void DeQueue(QUEUE &Q) { if(Empty(Q)) error("queue is empty"); else Q.front = addone(Q.front); }
部分资料来自《数据结构与算法--张岩》