链队
/*
初始化 入队 出队 输出
使用方法:
LQueue Q;
LQueue_init(&Q);
*/
#include <iostream>
#include <stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status是函数返回值类型,其值是函数结果状态代码
typedef int ElemType; //ElemType为可定义的数据类型,此设为int类型
typedef struct QNode {
ElemType data;
struct QNode *next;
} QNode, *Qptr;
typedef struct {
Qptr front;
Qptr rear;
int size;
} LQueue;
/**
* @brief 链队列的初始化
* @param LQueue类型
* @retval Status
*/
Status LQueue_init(LQueue *LQ) {
LQ->front = LQ->rear = new QNode;
LQ->front ->next = NULL;
return OK;
}
/**
* @brief 链队列入队
* @param LQueue类型,ElemType类型
* @retval Status
*/
Status LQueue_enter(LQueue *LQ, ElemType e) {
Qptr p = new QNode;
p->data = e;
p->next = NULL;
LQ->rear ->next = p;
LQ->rear = p;
return OK;
}
/**
* @brief 链队列出队
* @param LQueue类型
* @retval Status
*/
Status LQueue_del(LQueue *LQ) {
if (LQ->front == LQ->rear ) {
cout << "队列为空\n";
return ERROR;
}
Qptr p = LQ->front ->next;
LQ->front ->next = p->next ;
if (LQ->rear == p)
LQ->front = LQ->rear ;
delete p;
return OK;
}
/**
* @brief 链队列的输出
* @param LQueue类型
* @retval Status
*/
Status LQueue_output(LQueue *LQ) {
if (LQ->front == LQ->rear ) {
cout << "队列为空\n";
return ERROR;
}
Qptr p;
p = LQ->front ->next;
cout << "队列:";
while (p) {
cout << p->data << " ";
p = p->next ;
}
printf("\n");
return OK;
}
test
#include "LQueue.h"
int main() {
LQueue Q;
LQueue_init(&Q);
for (int i = 1; i < 10; ++i)
LQueue_enter(&Q, i);
for (int i = 1; i < 10; ++i)
LQueue_del(&Q);
LQueue_enter(&Q, 10);
LQueue_output(&Q);
return 0;
}
顺序队
/*
队列创建 求长度 入队 出队 输出
*/
#include <iostream>
#include <stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status是函数返回值类型,其值是函数结果状态代码
typedef int ElemType; //ElemType为可定义的数据类型,此设为int类型
typedef struct {
ElemType *base; //储存空间基地址
int front;
int rear;
int size;
} SqQueue;
/**
* @brief 队列创建
* @param SqQueue类型,队列大小
* @retval 无
*/
void SqQueue_create(SqQueue *Q, int size) {
Q->base = new ElemType[size];
if (!Q->base )
exit(OVERFLOW);
cout << "队列创建成功\n";
Q->front = Q->rear = 0;
Q->size = size;
}
/**
* @brief 求队列长度
* @param SqQueue类型
* @retval 队列长度
*/
int SqQueue_length(SqQueue *Q) {
return (Q->rear - Q->front);
}
/**
* @brief 元素进队
* @param SqQueue类型,ElemTypoe类型
* @retval Status
*/
Status SqQueue_enter(SqQueue *Q, ElemType e) {
if (Q->rear == Q->size ) {
cout << "队列已满,元素" << e << "入队失败\n";
return ERROR;
}
Q->base [Q->rear++ ] = e;
return OK;
}
/**
* @brief 元素出队
* @param SqQueue类型
* @retval Status
*/
Status SqQueue_del(SqQueue *Q) {
if (Q->front == Q->rear ) {
cout << "队列为空\n";
return ERROR;
}
Q->front ++;
return OK;
}
/**
* @brief 输出队列当前元素
* @param SqQueue类型
* @retval 无
*/
void SqQueue_output(SqQueue *Q) {
for (int i = Q->front ; i < Q->rear ; ++i) {
cout << Q->base [i] << " ";
}
cout << "\n";
}
test
#include <stdio.h>
#include "SqQueue.h"
int main() {
SqQueue Q;
SqQueue_create(&Q, 10);
SqQueue_enter(&Q, 3);
SqQueue_enter(&Q, 4);
SqQueue_enter(&Q, 5);
cout << SqQueue_length(&Q) << "\n";
SqQueue_del(&Q);
SqQueue_enter(&Q, 6);
SqQueue_enter(&Q, 8);
SqQueue_enter(&Q, 9);
SqQueue_enter(&Q, 10);
SqQueue_enter(&Q, 11);
SqQueue_enter(&Q, 12);
SqQueue_enter(&Q, 13);
SqQueue_enter(&Q, 14);
SqQueue_output(&Q);
return 0;
}