I have a question about this code #include<iostream> #include<cstdlib> #define M
ID: 3745984 • Letter: I
Question
I have a question about this code
#include<iostream>
#include<cstdlib>
#define MAX_SIZE
using namespace std;
class Queue {
private:
int thing[10];
int rear;
int front;
public:
Queue();
void enqueue(int);
int dequeue();
int size();
void display();
bool isEmpty();
bool isFull();
};
Queue::Queue() {
rear = 1; //originaly the rear is supposed to be -1 and the front is set to 0. I don't understand why the code doesn't work when it's set to to other numbers. please Explain
front = 10;
}
void Queue::enqueue(int data) {
thing[++rear] = data;
}
int Queue::dequeue() {
return thing[front++];
}
void Queue::display() {
if (!this -> isEmpty()) {
for (int i = front; i <= rear; i++)
cout << thing[i] << endl;
} else {
cout << "Queue Underflow" << endl;
}
}
int Queue::size() {
return (rear - front + 1);
}
bool Queue::isEmpty() {
if (front > rear) {
return true;
} else {
return false;
}
}
bool Queue::isFull() {
if (this -> size() >= 10) {
return true;
} else {
return false;
}
}
int main() {
Queue queue;
int choice, data;
while (1) {
cout <<endl<< endl<<"1. Enqueue"<<endl<<"2. Dequeue"<<endl<<"3. Size"<<endl<<"4. Display all element"<<endl<<"5. Quit";
cout<<endl<<"Enter your choice: "<<endl;
cin >> choice;
switch (choice) {
case 1:
if (!queue.isFull()) {
cout << endl<<"Enter data: ";
cin >> data;
queue.enqueue(data);
} else {
cout << "Queue is Full" << endl;
}
break;
case 2:
if (!queue.isEmpty()) {
cout << "The data dequeued is :" << queue.dequeue();
} else {
cout << "Queue is Emtpy" << endl;
}
break;
case 3:
cout << "Size of Queue is " << queue.size();
break;
case 4:
queue.display();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Explanation / Answer
// Explanation is highlighted in code
#include<iostream>
#include<cstdlib>
#define MAX_SIZE
using namespace std;
class Queue {
private:
int thing[10];
int rear;
int front;
public:
Queue();
void enqueue(int);
int dequeue();
int size();
void display();
bool isEmpty();
bool isFull();
};
Queue::Queue() {
rear = -1; //originaly the rear is supposed to be -1 and the front is set to 0.
// because rear and front corresponds to the index in array ( array "thing" declared at start of code in Queue class), hence when enqueue fuction is called rear acts as an index in array to store input values and front corresponds to index in Queue class pointing to the front element on Queue. Thus, no other number apart from -1 and 0 can be used.
front = 0;
}
void Queue::enqueue(int data) {
thing[++rear] = data;
}
int Queue::dequeue() {
return thing[front++];
}
void Queue::display() {
if (!this -> isEmpty()) {
for (int i = front; i <= rear; i++)
cout << thing[i] << endl;
} else {
cout << "Queue Underflow" << endl;
}
}
int Queue::size() {
return (rear - front + 1);
}
bool Queue::isEmpty() {
if (front > rear) {
return true;
} else {
return false;
}
}
bool Queue::isFull() {
if (this -> size() >= 10) {
return true;
} else {
return false;
}
}
int main() {
Queue queue;
int choice, data;
while (1) {
cout <<endl<< endl<<"1. Enqueue"<<endl<<"2. Dequeue"<<endl<<"3. Size"<<endl<<"4. Display all element"<<endl<<"5. Quit";
cout<<endl<<"Enter your choice: "<<endl;
cin >> choice;
switch (choice) {
case 1:
if (!queue.isFull()) {
cout << endl<<"Enter data: ";
cin >> data;
queue.enqueue(data);
} else {
cout << "Queue is Full" << endl;
}
break;
case 2:
if (!queue.isEmpty()) {
cout << "The data dequeued is :" << queue.dequeue();
} else {
cout << "Queue is Emtpy" << endl;
}
break;
case 3:
cout << "Size of Queue is " << queue.size();
break;
case 4:
queue.display();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.