赞
踩
目录
顺序队列是由一维数组实现的队列,其中队头元素的下标为0,队尾元素的下标为n-1(n为数组大小),队列的大小在创建时确定,且在队列的生命周期内保持不变。
顺序队列的优点:
顺序队列的缺点:
循环队列是一种线性数据结构,它将队列的元素存储在一个连续的数组中,并通过使用循环指针来管理队列的插入和删除操作。循环队列的特点在于,当队列为空时,头尾指针指向同一位置;当队列满时,头尾指针也指向同一位置。
循环队列的优点:
循环队列的缺点:
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -1
-
- #define MAXSIZE 100
-
- typedef int QElemType;
- typedef int Status;
- //创建结构体
- typedef struct {
- QElemType *base;
- int front;
- int rear;
- } SqQueue;
- //初始化
- Status InitQueue(SqQueue &Q) {
- //构造一个空队列
- Q.base = new QElemType[MAXSIZE];
- if (!Q.base) {
- exit(OVERFLOW);
- }
- Q.front = Q.rear = 0;
- return OK;
- }
- //销毁队列
- Status DestroyQueue(SqQueue &Q) {
- if (Q.base) {
- delete Q.base;
- }
- Q.base = NULL;
- Q.front = Q.rear = 0;
- return OK;
- }
- //清空队列
- Status ClearQueue(SqQueue &Q) {
- Q.front = Q.rear = 0;
- return OK;
- }
- //求长度
- Status QueueLength(SqQueue Q) {
- return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
-
- }
- Status QueueEmpty(SqQueue Q) {
- if (Q.front == Q.rear) {
- return true;
- } else {
- return false;
- }
- }
- //求队头元素
- Status GetHead(SqQueue Q, QElemType &e) {
- if (Q.front == Q.rear) {
- return ERROR;
- }
- e = Q.base[Q.front];
- return OK;
- }
- //循环队列入队
- Status EnQueue(SqQueue &Q, QElemType e) {
- if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR;
- Q.base[Q.rear] = e;
- Q.rear = (Q.rear + 1) % MAXSIZE;
- return OK;
- }
- //循环队列出队
- Status DeQueue(SqQueue &Q, QElemType &e) {
- if (Q.front == Q.rear) {
- return ERROR;
- }
- e = Q.base[Q.front];
- Q.front = (Q.front + 1) % MAXSIZE;
- return OK;
- }
- //遍历队列
- void DisplayQueue(SqQueue Q) {
- int i = Q.front;
- printf("队列元素为: ");
- while (Q.front != Q.rear && (i + MAXSIZE) % MAXSIZE != Q.rear) {
- printf("%d ", Q.base[i]);
- i++;
- }
- }
- #include <stdio.h>
- #include <stdlib.h>
-
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -1
-
- #define MAXSIZE 100
-
- typedef int QElemType;
- typedef int Status;
-
- //创建结构体
- typedef struct {
- QElemType *base;
- int front;
- int rear;
- } SqQueue;
-
- //初始化
- Status InitQueue(SqQueue &Q) {
- //构造一个空队列
- Q.base = new QElemType[MAXSIZE];
- if (!Q.base) {
- exit(OVERFLOW);
- }
- Q.front = Q.rear = 0;
- return OK;
- }
-
- //销毁队列
- Status DestroyQueue(SqQueue &Q) {
- if (Q.base) {
- delete Q.base;
- }
- Q.base = NULL;
- Q.front = Q.rear = 0;
- return OK;
- }
-
- //清空队列
- Status ClearQueue(SqQueue &Q) {
- Q.front = Q.rear = 0;
- return OK;
- }
-
- //求长度
- Status QueueLength(SqQueue Q) {
- return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
-
- }
-
- //判空
- Status QueueEmpty(SqQueue Q) {
- if (Q.front == Q.rear) {
- return true;
- } else {
- return false;
- }
- }
-
- //求队头元素
- Status GetHead(SqQueue Q, QElemType &e) {
- if (Q.front == Q.rear) {
- return ERROR;
- }
- e = Q.base[Q.front];
- return OK;
- }
-
- //循环队列入队
- Status EnQueue(SqQueue &Q, QElemType e) {
- if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR;
- Q.base[Q.rear] = e;
- Q.rear = (Q.rear + 1) % MAXSIZE;
- return OK;
- }
-
- //循环队列出队
- Status DeQueue(SqQueue &Q, QElemType &e) {
- if (Q.front == Q.rear) {
- return ERROR;
- }
- e = Q.base[Q.front];
- Q.front = (Q.front + 1) % MAXSIZE;
- return OK;
- }
-
- //遍历队列
- void DisplayQueue(SqQueue Q) {
- int i = Q.front;
- printf("队列元素为: ");
- while (Q.front != Q.rear && (i + MAXSIZE) % MAXSIZE != Q.rear) {
- printf("%d ", Q.base[i]);
- i++;
- }
- }
-
- //功能菜单列表
- void show_help() {
- printf("******* 功能菜单列表 *******\n");
- printf("1----入队------------------\n");
- printf("2----求循环队列长度----------\n");
- printf("3----出队------------------\n");
- printf("4----取队头元素-------------\n");
- printf("5----清空循环队列-----------\n");
- printf("6----销毁循环队列-----------\n");
- printf("7----判断循环队列是否为空-----\n");
- printf("8----批量插入元素------------\n");
- printf("9----显示队列元素------------\n");
- printf("10----退出------------------\n\n");
- }
-
- int main() {
- SqQueue sq;
-
- //初始化
- Status rInitStack = InitQueue(sq);
- if (rInitStack == OK) {
- printf("循环队列初始化成功!\n");
- } else {
- printf("循环队列初始化失败!\n");
- }
-
-
- while (1) {
-
- //功能菜单列表
- show_help();
-
- int flag;
- printf("请输入所需的功能编号:\n");
- scanf("%d", &flag);
-
- switch (flag) {
- case 1: {//入队
- Status EnQueueindex;
- printf("请输入插入元素(请在英文状态下输入例如:1): \n");
- scanf("%d", &EnQueueindex);
- Status rEnQueue = EnQueue(sq, EnQueueindex);
- if (rEnQueue == OK) {
- printf("向循环队列入队%d成功!\n", EnQueueindex);
- } else {
- printf("向循环队列入队失败!\n");
- }
- }
- break;
- case 2: {//求循环队列的长度
- int length = QueueLength(sq);
- printf("循环队列的长度为:%d。 \n\n", length);
- }
- break;
- case 3: {//出队
- Status DeQueueindex;
- Status rDeQueue = DeQueue(sq, DeQueueindex);
- if (rDeQueue == OK) {
- printf("向循环队列出队%d成功!\n", DeQueueindex);
- } else {
- printf("向循环队列出队失败!\n");
- }
- }
- break;
- case 4: {//求队头元素
- Status topData;
- Status rGetHead = GetHead(sq, topData);
- if (rGetHead == OK) {
- printf("向循环队列获取队头元素:%d\n", topData);
- } else {
- printf("向循环队列获取队头元素失败!\n");
- }
- }
- break;
- case 5: { //清空
- Status rClearStack = ClearQueue(sq);
- if (rClearStack == OK) {
- printf("循环队列清空成功!\n");
- } else {
- printf("循环队列清空失败!\n");
- }
- }
- break;
- case 6: {//销毁
- Status rDestroyStack = DestroyQueue(sq);
- if (rDestroyStack == OK) {
- printf("循环队列销毁成功!\n");
- } else {
- printf("循环队列销毁失败!\n");
- }
- }
- break;
- case 7: {///判空
- Status ClearStatus = QueueEmpty(sq);
- if (ClearStatus == true) {
- printf("循环队列为空!\n\n");
- } else {
- printf("循环队列不为空!\n\n");
- }
- }
- break;
- case 8: {//批量插入
- int on;
- printf("请输入想要插入的元素个数:\n");
- scanf("%d", &on);
- QElemType array[on];
- for (int i = 1; i <= on; i++) {
- printf("向循环队列第%d个位置插入元素为:", i);
- scanf("%d", &array[i]);
- }
-
- for (int i = 1; i <= on; i++) {
- Status InsertStatus = EnQueue(sq, array[i]);
- if (InsertStatus == OK) {
- printf("向循环队列第%d个位置插入元素%d成功!\n", i, array[i]);
- } else {
- printf("向循环队列第%d个位置插入元素%d失败!\n", i, array[i]);
- }
- }
- }
- break;
- case 9: {//输出链表元素
- DisplayQueue(sq);
- printf("\n");
- }
- break;
- case 10: {//退出程序
- return 0;
- }
- break;
- default: {
- printf("输入错误,无此功能,请检查输入:\n\n");
- }
- }
- }
-
- return 1;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。