@bekti/

cpp queue

C++

demo code for this blog article (https://bekti.net/blog/implementasi-queue-di-cpp)

fork
loading
Files
  • main.cpp

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7791301447982868","path":"main.cpp","file":{"path":"main.cpp","content":{"asEncoding":{"base64":"I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIE1BWCAyMCAvL21ha3NpbXVtIGRhdGEgcXVldWUKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vRGVrbGFyYXNpIHN0cnVjdCBhbnRyaWFuCnN0cnVjdCBRdWV1ZSB7CglpbnQgZnJvbnQsIHJlYXIsIGRhdGFbTUFYXTsKfVE7CgovL2NlayBhcGFrYWggYW50cmlhbiBwZW51aApib29sIGlzRnVsbCgpIHsKCXJldHVybiBRLnJlYXIgPT0gTUFYOwp9CgovL2NlayBhcGFrYWggYW50cmlhbiBrb3NvbmcKYm9vbCBpc0VtcHR5KCkgewoJcmV0dXJuIFEucmVhciA9PSAwOwp9CgovL01lbmFtcGlsa2FuIFF1ZXVlCnZvaWQgcHJpbnRRdWV1ZSgpIHsKCWlmIChpc0VtcHR5KCkpIHsKICAgIGNvdXQgPDwgIkFudHJpYW4ga29zb25nIjw8ZW5kbDsKCX0KCWVsc2UgewoJCWNvdXQgPDwgIlFVRVVFIDogIjsKCQlmb3IgKGludCBpID0gUS5mcm9udDsgaSA8IFEucmVhcjsgaSsrKQoJCS8vbWVuYW1iYWhrYW4ga29tYSBqaWthIGRhdGEgdGlkYWsgdGVyZGFwYXQgZGkgYW50cmlhbiBwZXJ0YW1hCgkJCWNvdXQgPDwgUS5kYXRhW2ldIDw8ICgoUS5yZWFyLTEgPT0gaSkgPyAiIiA6ICIsIik7CgkJY291dCA8PCBlbmRsOwogIH0KfQoKLy9tYW5hbWJhaGthbiBkYXRhIGtlIGFudHJpYW4Kdm9pZCBlbnF1ZXVlKCkgewoJaWYgKGlzRnVsbCgpKQoJewoJCWNvdXQgPDwgIk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ICIgWzFdIEVucXVldWUgXG4iCgkJCTw8ICIgWzJdIERlcXVldWVcbiIKCQkJPDwgIiBbM10gS2VsdWFyIFxuXG4iCgkJCTw8ICItLS0tLS0tLS0tLS0tLS0tLS0tXG4iCgkJCTw8ICJNYXN1a2thbiBwaWxpaGFuIDogIjsgY2luID4+IGNob29zZTsKCQlzd2l0Y2ggKGNob29zZSkKCQl7CgkJY2FzZSAxOgoJCQllbnF1ZXVlKCk7CgkJCWJyZWFrOwoJCWNhc2UgMjoKCQkJZGVxdWV1ZSgpOwoJCQlicmVhazsKCQlkZWZhdWx0OgoJCQljb3V0IDw8ICJQaWxpaGFuIHRpZGFrIHRlcnNlZGlhIjsKCQkJYnJlYWs7CgkJfQoJfSB3aGlsZSAoY2hvb3NlICE9Myk7CglyZXR1cm4gMDsKfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#define MAX 20 //maksimum data queue
using namespace std;

//Deklarasi struct antrian
struct Queue {
	int front, rear, data[MAX];
}Q;

//cek apakah antrian penuh
bool isFull() {
	return Q.rear == MAX;
}

//cek apakah antrian kosong
bool isEmpty() {
	return Q.rear == 0;
}

//Menampilkan Queue
void printQueue() {
	if (isEmpty()) {
    cout << "Antrian kosong"<<endl;
	}
	else {
		cout << "QUEUE : ";
		for (int i = Q.front; i < Q.rear; i++)
		//menambahkan koma jika data tidak terdapat di antrian pertama
			cout << Q.data[i] << ((Q.rear-1 == i) ? "" : ",");
		cout << endl;
  }
}

//manambahkan data ke antrian
void enqueue() {
	if (isFull())
	{
		cout << "Antrian penuh!"<<endl;
	}
	else {
		int data;
		//menambahkan data ke antrian
		cout << "Masukkan Data : ";cin >> data;
		Q.data[Q.rear] = data;
		//menempatkan tail pada elemen data terakhir yang ditambahkan
		Q.rear++;
		cout << "Data ditambahkan\n";
		printQueue();
	}
}

// mengambil data dari antrian
void dequeue() {
	if (isEmpty())
	{
		cout << "Antrian masih kosong"<<endl;
	}
	else{
		cout << "Mengambil data \"" << Q.data[Q.front] << "\"..." << endl;
		//menggeser antrian data ke head
		for (int i = Q.front; i < Q.rear; i++)
			Q.data[i] = Q.data[i + 1];
		//menempatkan tail pada data terakhir yang digeser
		Q.rear--;
		printQueue();
	}
}

int main() {
	int choose;
	do
	{
		//Tampilan menu
		cout << "-------------------\n"
			<< "   Menu Pilihan\n"
			<< "-------------------\n"
			<< " [1] Enqueue \n"
			<< " [2] Dequeue\n"
			<< " [3] Keluar \n\n"
			<< "-------------------\n"
			<< "Masukkan pilihan : "; cin >> choose;
		switch (choose)
		{
		case 1:
			enqueue();
			break;
		case 2:
			dequeue();
			break;
		default:
			cout << "Pilihan tidak tersedia";
			break;
		}
	} while (choose !=3);
	return 0;
}