链队列的存储实现(入队,出队,清队,销毁队,遍历,判断是否为空等)
具体实现代码如下:
/*链队列的入队,出队,清队,判断队空等***/
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
#define MAXSIZE 100
#define OVERFLOW -2
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 1
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front; //队头指针
QueuePtr rear; //队尾指针
}LinkQueue;
Status InitQueue(LinkQueue &Q);///构造一个空队列
Status DestroyQueue(LinkQueue &Q);///销毁队列Q,Q不再存在
Status ClearQueue(LinkQueue &Q);///将Q清为空队列
Status QueueEmpty(LinkQueue Q);///若队列Q为空,则返回TRUE,否则返回FALSE
int QueueLength(LinkQueue Q);///返回Q的元素个数,即为队列的长度
Status GetHead(LinkQueue Q, QElemType &e);///若队列不为空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
Status EnQueue(LinkQueue &Q, QElemType e);///插入元素e为Q的新的队尾元素
Status DeQueue(LinkQueue &Q, QElemType &e);///若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
void visit(QElemType e);///输出元素e
Status QueueTraverse(LinkQueue Q, void(*visit)(QElemType));///遍历队列Q
Status InitQueue(LinkQueue &Q)
{
///构造一个空队列Q
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q){
///销毁队列Q,Q不再存在
while(Q.front){
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return OK;
}
Status ClearQueue(LinkQueue &Q){
///将Q清为空队列
QueuePtr q,p;
p = Q.front->next;
Q.front->next = NULL;
while(p){
q = p->next;
free(p);
p = q;
}
Q.rear = Q.front;
return OK;
}
Status QueueEmpty(LinkQueue Q){
///判断队列是否为空
if(Q.front->next = NULL)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
{
///返回Q的元素个数,即队列的长度
int i = 0;
QueuePtr p;
p = Q.front->next;
while(p){
i++;
p = p->next;
}
return i;
}
Status GetHead(LinkQueue Q, QElemType &e){
///若队列不为空,则用e返回Q的队头元素,并返回OK;否则返回ERROR
QueuePtr p;
if(Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e)
{
///插入元素e,为Q的新的队尾元素
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p) return OVERFLOW;
p->data = e;
Q.rear->next = p;
Q.rear = p;
p->next = NULL;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e)
{
///若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
QueuePtr p;
if(Q.front = Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
free(p);
return OK;
}
void visit(QElemType e){
///输出元素e
cout << e << " ";
}
Status QueueTraverse(LinkQueue Q, void(*visit)(QElemType)){
///利用指针函数,遍历队列Q
QueuePtr q;
q = Q.front->next;
while(q){
visit(q->data);
q = q->next;
}
cout << endl;
return OK;
}
int main()
{
int i = 0,l;
Status j;
QElemType e;
LinkQueue Q;
InitQueue(Q);
if(!QueueEmpty(Q))
cout << "队列为空。" << endl;
cout << "请输入值到队列中,长度应小于" << MAXSIZE -1 << endl;
do{
cin >> e;
++i;
if(e == -1){
break;
}
EnQueue(Q,e);
}while(i < MAXSIZE -1);
cout << "队列的长度为:" << QueueLength(Q) << endl;
cout << "开始从队头删除元素,从队尾插入元素:" << endl;
for(i = 0; i < QueueLength(Q); ++i){
DeQueue(Q,e);
cout << "删除的元素为:" << e << ".请输入新的值进行插入:";
cin >> e;
EnQueue(Q,e);
}
l = QueueLength(Q);
cout << "队列的元素为:" << endl;
QueueTraverse(Q,visit);
j = GetHead(Q,e);
cout << "队列的首元素为:" << e << endl;
ClearQueue(Q);
if(!QueueEmpty(Q))
cout << "队列不为空!" << endl;
else
cout << "队列为空!" << endl;
DestroyQueue(Q);
return 0;
}