P!NK_MF 2022-04-01 15:16 采纳率: 60%
浏览 49
已结题

已经完成了队列的一系列基本操作,请问如何才能从键盘上输入元素至队列中呢?

typedef struct //队列的定义
{
char data[MAXSIZE];
int front;
int rear;
}Queue;

typedef int status;
status InitQueue(Queue &Q) //队列的初始化
{
Q.front=Q.rear=0;
return OK;
}

bool IsEmptyQ(Queue &Q) //判断队列是否为空
{
if (Q.front==Q.rear)
return true;
else
return false;
}

bool IsOverQ(Queue &Q) //判断队列是否为满
{
if (((Q.rear)%MAXSIZE)==Q.front) //这里就想到钟表来理解
return true;
else
return false;
}

status InQueue(Queue &Q,char x) //入队
{
if (IsOverQ(Q)) //队满不入
return ERROR;
else
printf("请输入要入队的元素:",x);
scanf("%d",&x);
Q.data[Q.rear]=x;
Q.rear++;
}

status OutQueue(Queue &Q,char &x) //出队
{
if (IsEmptyQ(Q)) //队空不出
return ERROR;
else
printf("%d",Q.front);
Q.front++;
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-04-01 16:17
    关注

    修改如下,供参考:

    #include <stdio.h>
    #define OK 1
    #define ERROR 0
    #define MAXSIZE 50
    typedef struct queue//队列的定义
    {
        char data[MAXSIZE];
        int front;
        int rear;
    }Queue;
    typedef int status;
    status InitQueue(Queue& Q) //队列的初始化
    {
        Q.front = Q.rear = 0;
        return OK;
    }
    
    bool IsEmptyQ(Queue& Q) //判断队列是否为空
    {
        if (Q.front == Q.rear)
            return true;
        else
            return false;
    }
    
    bool IsOverQ(Queue& Q) //判断队列是否为满
    {
        if ((Q.rear - Q.front) > MAXSIZE) 
        //if (((Q.rear)%MAXSIZE)==Q.front) //这里就想到钟表来理解
            return true;
        else
            return false;
    }
    
    status InQueue(Queue& Q, char  x) //入队
    {
        if (IsOverQ(Q)) //队满不入
            return ERROR;
        else{
            //printf("请输入要入队的元素:", x);
            //scanf("%d", &x);
            Q.data[Q.rear] = x;
            Q.rear++;
        }
        return OK;
    }
    status OutQueue(Queue& Q, char & x) //出队
    {
        if (IsEmptyQ(Q)) //队空不出
            return ERROR;
        else{
            x = Q.data[Q.front];  
            Q.front++;
        }
        return OK;
    }
    int main()
    {
        int i;
        char  ch;
        Queue Q;
        InitQueue(Q);
        for (i = 0; i < 10; i++) //入队
            InQueue(Q, i + 'A');
    
        for (i = 0; i < 10; i++){//出队
            OutQueue(Q, ch);
            printf("%c\n", ch);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 4月1日

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程