当我运行delete函数时,它给了我一些垃圾值到无穷大。问题是什么?
#include<iostream>
#include<process.h>
#include<conio.h>
using namespace std;
class circularqueue
{
int arr[5];
int front,rear;
int size;
int count;
public:
circularqueue()
{
count = -1;
front=-1;
rear=-1;
size=5;
}
void display();
void enqueue(int val);
void delete_element();
bool Is_empty();
};
bool circularqueue::Is_empty(){
if(front == -1 && rear == -1){
return true;
}
else {
return false;
}
}
void circularqueue :: display()
{
int F = front;
for(count; count!=0;count--){
cout<<"-----------"<<endl;
cout<<"ID is : "<<arr[F]<<endl;
cout<<"-----------"<<endl;
F++;
if(count == (size-1)){
F=0;
}
else if(count == -1){
cout<<"Empty"<<endl;
}
}
}
void circularqueue :: enqueue(int val)
{
if(Is_empty()){
// cout<<"Queue is empty "<<endl;
++front;
++rear;
arr[rear] = val;
}
else if(front == 0 and rear == (size-1)){
cout<<"Queue is Full"<<endl;
}
else if(front==0 && rear == 0){
if(rear<=(size-1)){
arr[rear]= val;
rear = rear+1;
}
}
else if(front!=0 && rear == (size-1)){
rear=0;
arr[rear]=val;
rear =rear+1;
}
else {
arr[rear] = val;
rear=rear+1;
}
count++;
}
当我运行此代码时,删除功能无法正常工作,请告诉我修复。
它给了我一些垃圾值,它看起来像一个无限循环
void circularqueue :: delete_element()
{
if(Is_empty()){
cout<<"The Queue is empty"<<endl;
}
else if(front==(size-1)){
front = 0;
}
else if(front == rear){
front = -1;
rear = -1;
}
else
{
cout<<"Number Deleted : "<<front<<endl;
front = front+1;
}
count--;
}
int main()
{
circularqueue obj;
int num;
int ch;
char ch1;
jb: cout<<" 1 - EnQUEue 2- DeQueue 3- Display"<<endl;
cin>>ch;
switch(ch){
case 1:{
cout<<"Enter Number : "<<endl;
cin>>num;
obj.enqueue(num);
cout<<"Return to Main Menu: "<<endl;
cin>>ch1;
if(ch1=='Y'|| ch1=='y'){
system("cls");
goto jb;
}
break;
}
case 2:{
obj.delete_element();
cout<<"Return to Main Menu: "<<endl;
cin>>ch1;
if(ch1=='Y'|| ch1=='y'){
system("cls");
goto jb;
}
break;
}
case 3:{
obj.display();
cout<<"Return to Main Menu: "<<endl;
cin>>ch1;
if(ch1=='Y'|| ch1=='y'){
system("cls");
goto jb;
}
break;
}
default :{
cout<<"Invalid entery "<<endl;
cout<<"Return to Main Menu: "<<endl;
cin>>ch1;
if(ch1=='Y'|| ch1=='y'){
system("cls");
goto jb;
}
break;
}
}
}
答案 0 :(得分:0)
我已经制作了一个代码的压缩版本,它执行相同的任务。只是看看这是否适合你。
:map