السلام عليكم
لدي برنامج اريد من يساعدني على فهمه
تعودت دائما على حل الطوابير بالــ struct
ولكن واجهني هذا المثال تم حله بالــ class
مالفرق بين الطريقتين؟؟
#include <iostream.h>
#define SIZE 20
class queue
{
int *Queue, front, rear;
public:
queue() {
Queue = new int[SIZE];
front = rear = -1;
}
void push() {
if (rear == (SIZE-1)) {
cout<<"\n Overflow!";
} else {
rear++;
cout<<"\n Enter element: ";
cin>>Queue[rear];
}
}
void pop() {
if (front == rear) {
cout<<"\n Underflow!";
} else {
cout<<"\nElement popped: "<<Queue[++front];
}
}
void display() {
if (front == rear) {
cout<<"\n Queue Empty";
} else {
for(int i = (front+1); i<=rear; i++) {
cout<<Queue<<" ";
}
}
}
};
int main()
{
int choice;
queue q;
while(choice != 4)
{
cout<<"\n\n Enter your choice :"
<<"\n 1. Push an element into Queue."
<<"\n 2. Pop an element from Queue."
<<"\n 3. Display the Queue."
<<"\n 4. Exit the program.\n\n";
cin>>choice;
switch (choice) {
case 1:
q.push();
break;
case 2:
q.pop();
break;
case 3:
q.display();
break;
case 4:
break;}
}}