2016-12-05 69 views
-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; 
     } 
    } 
} 
+0

你會壓縮這個問題嗎?嘗試指出您認爲代碼有誤的地方,以便其他人更容易回答 –

+0

請使用[最小,完整和可驗證的示例]更新您的文章(http://stackoverflow.com/help/) MCVE)。 –

+0

非常感謝通知,你只需要關注刪除功能,我認爲有問題 –

回答

0

我使你的代碼更緊湊的版本,做同樣的task.Just看看這對你的作品。

#include<iostream> 
using namespace std; 

class CircularQueue{ 
    int arr[10], front, rear, size; 
    const int max_size=sizeof(arr)/sizeof(int); 
public: 
    CircularQueue(){ front = rear = size = 0; } 

    bool isEmpty(){ return front==rear; } // or simply size == 0 

    bool isFull(){ return (rear+1)%max_size == front; } // or simply size == max_size - 1 

    void enQueue(int val){ 
     if(isFull()){ 
      cout << "Queue is Full" << endl; 
      return; 
     } 
     arr[rear] = val; 
     rear = (rear+1)%max_size; 
     size++; 
    } 
    int deQueue(){ 
     if(isEmpty()){ 
      cout << "Queue is Empty" << endl; 
      return -1; 
     } 
     int ans = arr[front]; 
     front = (front+1)%max_size; 
     size--; 
     return ans; 
    } 

    void display(){ 
     if(isEmpty()){ 
      cout << "nothing to display" << endl; 
      return; 
     } 
     for(int i=0; i<size; i++){ 
      cout<<"-----------"<<endl; 
      cout<<"ID is : "<<arr[(i+front)%max_size]<<endl; 
      cout<<"-----------"<<endl; 
     } 
    } 

}; 

int main(){ 
    CircularQueue ob; 
    bool break_loop=false; 
    while(!break_loop){ 

     cout << "1 Enqueue, 2 Dequeue, 3 Display 4 Quit \nOption = "; 
     int input; cin >> input; 
     int tmp; 
     switch(input){ 
      case 1 : 
       cout << "Enter number " << endl; 
       cin >> tmp; 
       ob.enQueue(tmp); 
       break; 
      case 2 : 
       tmp = ob.deQueue(); 
       if(tmp != -1) cout << "Element " << tmp << " deleted" <<endl; 
       break; 
      case 3 : 
       ob.display(); 
       break; 
      case 4 : 
       break_loop = true; 
       break; 
      default : 
       break; 
     } 
    } 
    return 0; 
} 
+0

我寫這個是因爲你有不必要的複雜代碼,如果有任何疑問可以在代碼中添加評論我會解釋它 –