#include <iostream>
#define MAX 10
using namespace std;
// B <- C <- A <-
typedef enum {
B = 1,
C = 2,
A = 3,
NOTHING = 0
} pengunjung;
class Queue {
pengunjung data[MAX];
int index;
public:
Queue();
void enqueue(pengunjung);
char dequeue();
void show();
};
Queue::Queue() {
index = 0;
for(int i = 0; i < MAX; i++) {
data[i] = NOTHING;
}
}
void Queue::enqueue(pengunjung x) {
pengunjung t1 = x, t2;
if(index < MAX) {
if(index == 0) {
data[index] = x;
index++;
} else {
for(int i = 0; i <= index; i++) {
if(data[i] > t1 || data[i] == NOTHING) {
t2 = data[i];
data[i] = t1;
t1 = t2;
}
}
index++;
}
}
}
char Queue::dequeue() {
pengunjung out = data[0];
for(int i=0; i<index; i++) {
data[i] = data[i+1];
if(data[i] == index-1) data[i] = NOTHING;
}
if(out == 3) return 'A';
else if(out == 2) return 'C';
else return 'B';
}
void Queue::show() {
if(index == 0) cout << "Antrian Kosong!";
for(int i=0; i<index; i++) {
if(data[i] == 3) cout << "A <- ";
else if(data[i] == 2) cout << "C <- ";
else if(data[i] == 1) cout << "B <- ";
}
cout << endl;
}
int main() {
Queue a;
a.enqueue(A);
a.show();
a.enqueue(C);
a.show();
a.enqueue(B);
a.show();
cout << a.dequeue() << " Keluar\n";
// a.show();
a.enqueue(C);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
a.enqueue(A);
a.show();
a.enqueue(C);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
a.enqueue(A);
a.show();
a.enqueue(B);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
return 0;
}
#define MAX 10
using namespace std;
// B <- C <- A <-
typedef enum {
B = 1,
C = 2,
A = 3,
NOTHING = 0
} pengunjung;
class Queue {
pengunjung data[MAX];
int index;
public:
Queue();
void enqueue(pengunjung);
char dequeue();
void show();
};
Queue::Queue() {
index = 0;
for(int i = 0; i < MAX; i++) {
data[i] = NOTHING;
}
}
void Queue::enqueue(pengunjung x) {
pengunjung t1 = x, t2;
if(index < MAX) {
if(index == 0) {
data[index] = x;
index++;
} else {
for(int i = 0; i <= index; i++) {
if(data[i] > t1 || data[i] == NOTHING) {
t2 = data[i];
data[i] = t1;
t1 = t2;
}
}
index++;
}
}
}
char Queue::dequeue() {
pengunjung out = data[0];
for(int i=0; i<index; i++) {
data[i] = data[i+1];
if(data[i] == index-1) data[i] = NOTHING;
}
if(out == 3) return 'A';
else if(out == 2) return 'C';
else return 'B';
}
void Queue::show() {
if(index == 0) cout << "Antrian Kosong!";
for(int i=0; i<index; i++) {
if(data[i] == 3) cout << "A <- ";
else if(data[i] == 2) cout << "C <- ";
else if(data[i] == 1) cout << "B <- ";
}
cout << endl;
}
int main() {
Queue a;
a.enqueue(A);
a.show();
a.enqueue(C);
a.show();
a.enqueue(B);
a.show();
cout << a.dequeue() << " Keluar\n";
// a.show();
a.enqueue(C);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
a.enqueue(A);
a.show();
a.enqueue(C);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
a.enqueue(A);
a.show();
a.enqueue(B);
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
cout << a.dequeue() << " Keluar\n";
a.show();
return 0;
}
Tidak ada komentar:
Posting Komentar