從作為數組實現的隊列中刪除元素時的邏輯錯誤

[英]logical error in deletion of an element from a queue implemented as an array


I was making this program for implementing a queue as an array, i am able to successfully insert values into the queue, but when i go for deletion of an element, the following happens : suppose i inserted the following into the queue : 1,2,3 now i wish to delete an element(the whole thing happens according to FIFO mechanism as we all know)

我正在制作這個程序來實現隊列作為數組,我能夠成功地將值插入到隊列中,但是當我去刪除元素時,會發生以下情況:假設我將以下內容插入隊列:1,2 ,3現在我想刪除一個元素(整個事情根據FIFO機制發生,我們都知道)

so, expected output : 2,3

所以,預期產量:2,3

actual output : (blank)

實際輸出:(空白)

I HAVE AN IMAGE ATTACHED(OF THE OUTPUT SCREEN) AS WELL.

我有一個附着的圖像(輸出屏幕)。

i think that it may be a logical error, i cant figure it out. I would be glad if anyone of you helped me out. Thanks!!

我認為這可能是一個邏輯錯誤,我無法弄明白。如果你們中的任何人幫助過我,我會很高興的。謝謝!!

THE CODE GOES LIKE : This image

代碼就像:這張圖片

#include<iostream>
#include<conio.h>
using namespace std;

int Remove(int[]);
int Insert(int[], int);
void Display(int[], int, int);

int front = -1, rear = -1;

void main()
{
    const int size = 50;
    int Q[size];
    int item, res;
    char ch = 'y';
    while (ch == 'y' || ch == 'Y')
    {
        cout << "\n Enter item for insertion : ";
        cin >> item;
        res = Insert(Q, item);
        if (res == -1)
        {
            cout << "\n OVERFLOW!";
            _getch();
        }
        cout << "\n Now the Queue is : ";
        Display(Q, front, rear);
        cout << "\n Do you wish to insert more elemens into the array(y/n)? 
: ";
        cin >> ch;
    }
    cout << "\n Now the deletion of elements from the Queue begins....";
    ch = 'y';
    while (ch == 'y' || ch == 'Y')
    {
        res = Remove(Q);
        if (res == -1)
        {
            cout << "\n UNDERFLOW!";
            _getch();
        }
        else
        {
            cout << "\n The element deleted from the Queue is : " << res << 
endl;
            cout << "\n Now, the Queue(Front--to--Rear) is : ";
            Display(Q, front, rear);
        }
        cout << "\n Do you wish to delete more elements from the Queue(y/n)? 
: ";
        cin >> ch;
    }
    _getch();
    return;
}

int Insert(int Q[], int ele)
{

    const int size = 50;
    if (rear == size - 1)
    {
        return -1;
    }
    else
    {
        if (rear == -1)
        {
            front = rear = 0;
            Q[rear] = ele;
        }
        else
        {
            rear++;
            Q[rear]=ele;
        }
        return 0;
    }
}

int Remove(int Q[])
{
    int ret;
    if (front == -1)
    {
        return -1;
    }
    else
    {
        ret = Q[front];
        if (front = rear)
        {
            front = rear = -1;
        }
        else
        {
            front++;
        }
    }
    return ret;
}

void Display(int Q[], int front, int rear)
{
    if (front == -1)
    {
        return;
    }
    for (int i = front; i < rear; i++)
    {
        cout << Q[i] << " <- ";
    }
    cout << Q[rear] << endl;
}

1 个解决方案

#1


0  

mistake is you are assigning rear to front in if condition . if it successfully assign's value then it will execute statement below if that means it will set front and rear as -1.

錯誤是你在條件下分配后方到前方。如果它成功分配了值,那么它將執行下面的語句,如果這意味着它將前后設置為-1。

 if (front = rear)

it should be like this

它應該是這樣的

 if (front == rear)

try changing condition in for loop like this

嘗試像這樣改變for循環中的條件

 for (int i = front; i <= rear; i++)
    {
        cout << Q[i] << " <- ";
    }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/01/26/729b3d394765e27ce1a857eac70f4872.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com