c - 当我尝试插入第二个元素时,循环队列插入元素问题显示队列已满
问题描述
在我的循环队列中,当我输入第一个元素时,它完美地进入了循环队列,但是当我在队列中输入第二个元素时,它显示队列已满,而数组的大小为 5,即使我调试它,我在输入第一个元素后发现它给出队列已满 '''
#include<stdio.h>
#include<stdlib.h>
# define SIZE 5
int cqueue[SIZE];
int front = -1;
int rear = -1;
void insert(int);
void main()
{
int ch,item;
while(1)
{
printf("\n select choice from given below operations of circular queue : ");
printf("\n 1.insert element");
printf("\n 2. delete element");
printf("\n 3. display elements");
printf("\n 4. exit");
printf("\n enter choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("enter element : ");
scanf("%d",&item);
insert(item);
break;
case 4 : exit(0);
default: printf("\ninvalid choice! enter again\n");
}
}
}
void insert(int ele)
{
if(front=rear+1 || SIZE==rear+1)
{
printf("\n circular queue is full ");
}
else if((rear==-1)&&(front==-1))
{
front=rear=0;
cqueue[rear]=ele;
}
else if(rear==SIZE-1)
{
rear=0;
cqueue[rear]=ele;
}
else
{
rear++;
cqueue[rear]=ele;
}
}
'''
解决方案
在插入功能中,您有
if(front=rear+1 || SIZE==rear+1)
. 将其更改为if(front == rear + 1 ...
. 所以你基本上每次被调用时都分配rear + 1
给变量。front
insert
推荐阅读
- excel - 为什么单击保存按钮时我的 vba 用户窗体会崩溃?
- java - Mapstruct - 忽略集合内的属性
- python - 使用 selenium 使用 python 获取当前日期的全尺寸屏幕截图
- javascript - Node.js/MongoDB 执行多关键字搜索
- kubernetes - 如何使用 terraform 从 S3 加载 kubeconfig 文件
- python - 使用 Flask-SQLAlchemy 将 Python zip() 列表加载到 SQLite DB
- php - 使用 Sanctum 的 Laravel 7 SPA API 身份验证
- java - JNA 将等效的 swift 指针从 Java Android 传递给 C
- swift - 从静态变量访问实例函数
- visual-studio-code - 如何防止为远程 ssh 连接启动新的 VS Code 窗口