C语言C++顺序数组循环队列的软件实现代码

12 篇文章 0 订阅
2 篇文章 0 订阅
/*
顺序循环队列
UP @hey超级巨星
*/
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define True 1
#define False 0
#define MAXSIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct
{
 Elemtype* base;
 Elemtype front;
 Elemtype rear;
 int maxsize=0;
}Queue;
//初始化函数
Status InitQueue(Queue &Q,int capacity)
{
 Q.base = (Elemtype*)malloc(sizeof(Elemtype)*capacity);
 if (Q.base == NULL)
  return ERROR;
 Q.front = Q.rear = 0;
 Q.maxsize = capacity;
 return OK;
}
//判满函数
Status isEmptyQueue(Queue &Q)
{
 if (Q.front == Q.rear)
  return True;
 else return False;
}
//判空函数
Status isFullQueue(Queue  &Q)
{//此队列满状态为最后空一个格放rear
 if ((Q.rear + 1)% Q.maxsize== Q.front)
  return True;
 return False;
}
//进入队列
Status InsertQueue(Queue &Q,Elemtype e)
{
 if (isFullQueue(Q))
 {
  printf("队列已满,无法插入");
  return ERROR;
 }
 Q.base[Q.rear] = e;
 Q.rear = (Q.rear + 1) % Q.maxsize;
 return OK;
}
//出队列
Elemtype DeQueue(Queue& Q)
{
 Elemtype e;
 if (isEmptyQueue(Q))
 {
  printf("空队列");
  return ERROR;
 }
 e = Q.base[Q.front];
 Q.front = (Q.front + 1) % Q.maxsize;
 return e;
}
//遍历队列
void TravQueue(Queue &Q)
{
 if (Q.front == Q.rear)
 {
  printf("此队列为空\n");
 }
 else
 {
  Elemtype p = Q.front;
  do
  {
   printf("%d\t", Q.base[p]);
   p = (p + 1) % Q.maxsize;
  } while(p!=Q.rear);
 }
}
//初始化赋值交互
Queue Initinteract()
{
 printf("welcome to the Sequence Queue\n");
 printf("你好 主人 是否开始创建队列,是回答 1 ,否回答2\n ");
 int on;
 cin >> on;
 if (on != 1 && on != 2)
 {
  printf("输入值不合法\n");
  exit(0);
 }
 if (on == 2)
  exit(0);
 else
 {
  system("cls");
  printf("请输入您想创建的容量\n");
  int cap;
  cin >> cap;
  Queue Q;
  int tag2 = 0;
  tag2 = InitQueue(Q, cap+1);
  if (tag2 != 1)
   exit(0);
  printf("顺序循环队列创建完毕\n");
  printf("开始输入%d个数", cap);
  int i=1;
  while (i<=cap)
  {
   printf("第%d个:", i);
   cin >> Q.base[i-1];
   Q.rear = Q.rear + 1;
   system("cls");
   i++;
  }
  return Q;
 }
}
int main()
{
 int sat;
 //初始化交互
 Queue Q = Initinteract();
 TravQueue(Q);
 //操作交互
 int loc, val;
 char m;
 while (1)
 {
   printf("\n插入请按A,弹出值请按B,遍历请C");
   //system("cls");
   cin >> m;
   switch (m)
   {
   case 'A':
    printf("开始插入,请输入插入值\n");
    scanf_s("%d", &val);
    sat = InsertQueue(Q,val);
    break;
   case 'B':
    printf("开始删除\n");
    printf("已弹出值%d\n", DeQueue(Q));
    break;
   case 'C':
    printf("当前数组值如下");
    TravQueue(Q);
    break;
   default:
    exit(0);
   }
 }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值