Uploaded by User94505

Algoritma dan Struktur Data - QUEUE

advertisement
ALGORITMA DAN STRUKTUR DATA
Judul
Nama
NRP
Dosen
Tanggal
: Queue
: Kevin Rafi Kurniawan
: 2210191011
: Maretha Ruswiansari, S.ST., M.T
: 9 Oktober 2020
Linearqueue.cpp
#include <iostream>
#include <stdio.h>
#define n 5
using namespace std;
int F, R, Q[n];
void init(){
F = 0;
R = -1;
}
void enQueue(int A){
if(R < n - 1){
R++;
Q[R] = A;
}else{
cout << "antrian penuh" << endl;
}
}
void deQueue(){
int A;
if(F < R + 1){
A = Q[F];
F++;
if(F == n){
F = 0;
R = -1;
}
}else{
cout << "antrian kosong" << endl;
}
}
void display(){
for(int i = F; i <= R; i++){
cout << Q[i] << ", ";
}
cout << endl;
}
int main(){
init();
enQueue(1);
enQueue(2);
display();
deQueue();
display();
return 0;
}
Circularqueue.cpp
#include <iostream>
#include <stdio.h>
#define n 5
using namespace std;
int F, R, Q[n], counter;
void init(){
F = 0;
R = -1;
counter = 0;
}
void enQueue(int A){
if(counter < n){
R = (R + 1) % n;
Q[R] = A;
counter++;
}else{
cout << "antrian penuh" << endl;
}
}
void deQueue(){
int A;
if(counter > 0){
A = Q[F];
F = (F + 1) % n;
counter--;
}else{
cout << "antrian kosong" << endl;
}
}
void display(){
// cout << F << "; " << R << endl;
// for(int i = F; i == R; i = (i + 1) % n){
//
cout << Q[i] << ", ";
// }
int x = 0;
int i = F;
while(x != 2){
cout << Q[i] << ", ";
i = (i + 1) % n;
if(x == 1) x++;
if(i == R) x++;
}
cout << endl;
}
int main(){
init();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();
deQueue();
deQueue();
enQueue(6);
enQueue(7);
display();
return 0;
}
Download