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)
so, expected output : 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 : enter image description here
#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;
}
Aucun commentaire:
Enregistrer un commentaire