📄 queue.h
字号:
#include "iostream.h"
#include "stdio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front; //队列的头指针
QueuePtr rear;// 队列的尾指针
}LinkQueue;
Status InitQueue (LinkQueue &Q) {
// 构造一个空队列Q
Q.front = Q.rear = new QNode;
if (!Q.front) return OVERFLOW; //存储分配失败
Q.front->next = NULL;
return OK;
}
Status DestoryQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear) return TRUE;
else return FALSE;
}
int QueueLength(LinkQueue Q)
{
QNode *p; int i=0;
p=Q.front->next;
while(p!=NULL)
{
p=p->next; i++;
}
return i;
}
Status EnQueue (LinkQueue &Q, QElemType e) {
// 插入元素e为Q的新的队尾元素
QNode *p;
p = new QNode;
if (!p) return OVERFLOW; //存储分配失败
p->data = e; p->next = NULL;
Q.rear->next = p; Q.rear = p;
return OK;
}
Status DeQueue (LinkQueue &Q, QElemType &e) {
// 若队列不空,则删除Q的队头元素,用 e 返回其值,并返回OK;否则返回ERROR
QNode *p;
if (Q.front == Q.rear) return ERROR;
p = Q.front->next; e = p->data;
Q.front->next = p->next;
if (Q.rear == p) Q.rear = Q.front;
delete (p); return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -