StarZZZZ5 2021-08-09 22:56 采纳率: 100%
浏览 30
已结题

创建一个队列,怎么实现不了呢

#include<stdio.h>
#include<stdlib.h>
typedef struct SQueue
{
int *arr;
int front;
int rear;
}SQueue;
//创建队列
void InitSQueue(SQueue *Q,int size)
{
Q = (SQueue *)malloc(sizeof(SQueue));
Q->arr = (int *)malloc(sizeof(size));
Q->front = 0;
Q->rear = 0;
}
//判断队满
bool isfull(SQueue *Q,int size)
{
if((Q->rear+1) % size == Q->front)
{
return true;
}
else
{
return false;
}
}
//判断队空
bool isEmpty(SQueue *Q,int size)
{
if(Q->front == Q->rear)
{
return true;
}
else
{
return false;
}
}
//入队
void EnQueue(SQueue *Q,int size,int x)
{
if(isfull(Q,size))
{
printf("队列已经满了,无法入队");
exit(0);
}
else
{

    Q->arr[Q->rear] = x;
    Q->rear = (Q->rear + 1) % size;    

}    

}
//出队
void DeQueue(SQueue *Q,int size,int &x)
{
if(isEmpty(Q,size))
{
printf("队内已经没有元素,无法出队");
}
else
{
x = Q->arr[Q->front];
Q->front = (Q->front + 1) % size;
}
}
//获取队头元素
void Gethead(SQueue *Q,int size,int &x)
{
x = Q->arr[Q->front];
}

int main()
{
SQueue *Q1;
InitSQueue(Q1,10);
EnQueue(Q1,10,5);
EnQueue(Q1,10,6);
int x;
DeQueue(Q1,10,x);
printf("出队元素:",x);
Gethead(Q1,10,x);
printf("队头元素:",x);
return 0;
}

  • 写回答

1条回答 默认 最新

  • weixin_52833540 2021-08-09 23:44
    关注

    Q->arr = (int *)malloc(sizeof(size));这一句还没生成数组,可以将sizeof(size)乘以一个数,来实现你需要的数组空间大小

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月18日
  • 已采纳回答 8月10日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state