我试图运行它,无论我插入队列的是什么以及插入的数量是多少,它都不会显示循环队列。
这些是我想要澄清和/或纠正的一些要点,我们希望得到回应。
在下面的代码中,在函数Insert_in_Cqueue()中,最后,有一个语句'后面的++; Cqueue [后方] = ele;' ,后方++表示我们是从后方向前移动,因为这是我们在循环队列中插入的方式吗?
代码:
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
int Insert_in_CQueue(int[], int);
void Display(int[], int, int);
int Del_from_CQueue(int CQueue[]);
void main()
{
const int size = 7;
int CQueue[size];
int Item, res, ch, front = -1, rear = -1;
do
{
cout << "\t\t\t Circular Queue Menu\n";
cout << "\t 1. Insert\n";
cout << "\t 2. Delete\n";
cout << "\t 3. Display\n";
cout << "\t 4. Exit\n";
cout << "\n Enter your choice(1-4)";
cin >> ch;
switch (ch)
{
case 1: cout << "\t\t You have selected the option to 'Insert' in the Queue. ";
cout << "\n Enter item for insertion ";
cin >> Item;
res = Insert_in_CQueue(CQueue, Item);
if (res == -1)
{
cout << "\n Overflow encountered";
}
else
{
cout << "\n Now the circular Queue is : ";
Display(CQueue, front, rear);
}
break;
case 2: Item = Del_from_CQueue(CQueue);
cout << "\n Element deleted is : " << Item << endl;
Display(CQueue, front, rear);
break;
case 3: Display(CQueue, front, rear);
break;
case 4: break;
default: cout << "\n Valid choices are 1,2,3,4 only ";
break;
}
} while (ch != 4);
_getch();
return;
}
int Insert_in_CQueue(int CQueue[], int ele)
{
int front = -1, rear = -1;
const int size = 7;
if ((front == 0 && rear == (size - 1)) || (front == rear + 1)) //the
second condition, as it is a circular queue, no end
{
return -1;
}
else if (rear == -1)
{
front = rear = 0;
}
else if (rear == size - 1)
{
rear = 0;
}
else
rear++;
CQueue[rear] = ele;
return 0;
}
void Display(int CQueue[], int front, int rear)
{
int i = 0;
const int size = 50;
cout << "\n Circular Queue is : \n" << "(Front shown as >>>, Rear as <<<
AND Free space as->\n";
if (front == -1)
return;
if (rear >= front)
{
for (i = 0; i < front; i++) cout << "-";
cout << ">>>";
for (i = 0; i < rear; i++) cout << CQueue[i] << "<---";
cout << CQueue[rear] << "<<<" << endl;
}
else
{
for (i = 0; i < rear; i++) cout << CQueue[i] << "<---";
cout << CQueue[rear] << "<<<";
for (i = 0; i < front; i++) cout << "-";
cout << ">>>";
for (i = front; i < size; i++) cout << CQueue[i] << "<---";
}
}
int Del_from_CQueue(int CQueue[])
{
int ret;
int front = -1, rear = -1;
const int size = 50;
if (front == -1) return -1;
else
{
ret = CQueue[front];
if (front == rear) front = rear = -1;
else if (front == size - 1)
front=0;
else front++;
}
return ret;
}
答案 0 :(得分:0)
检查循环队列是否已满的标准方法是
if((rear+1)%size == front)
Queue is Full.
检查循环队列是否为空的标准方法是
if(rear==front)
Queue is Empty.
请记住:在循环队列中,当一个空格仍为空时,队列被视为已满。这样做是为了区分空和满的条件。
插入或删除值时,不会简单地增加front
和rear
。当前端和后端的值达到队列大小时,它们必须为零。
例如:如果队列大小为7,当front
或rear
达到值6时,下一个值应该为零而不是7
因此,在插入时,您应该使用:
rear=(rear+1)%size;
CQueue[rear] = ele;
删除时,您应该使用:
ret = CQueue[front];
front=(front+1)%size;
您的回答: 是的!当循环队列为空时,前部和后部都在一起。当我们在队列中插入元素时,后部增加,前部保持不变。插入时增加后部值并不意味着我们从后面向前移动,但是当队列接近满时,前部和后部会合在一起。 例如:如果从起点开始在圆形路径中运行,则首先离开起点,但是当您倾向于完成运行时,您将从后面接近起始线。循环队列也是如此。