-1
當我運行刪除函數時,它給了我一些無窮大的垃圾值。有什麼問題?我的DeQueue()函數工作不正常
#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;
}
}
}
你會壓縮這個問題嗎?嘗試指出您認爲代碼有誤的地方,以便其他人更容易回答 –
請使用[最小,完整和可驗證的示例]更新您的文章(http://stackoverflow.com/help/) MCVE)。 –
非常感謝通知,你只需要關注刪除功能,我認爲有問題 –