Antrian

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Scanner; public class Main { // Kode lainnya... public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numb = 1; Queue queue = new Queue(); while (true) { System.out.println("\nPilih operasi antrian:"); System.out.println("1. Ambil Nomor"); System.out.println("2. Lanjut Antrian"); System.out.println("3. Panggil Antrian"); System.out.println("4. Jumlah Antrian"); System.out.println("0. Keluar"); System.out.print("Pilihan Anda: "); int choice = scanner.nextInt(); switch (choice) { case 1: //System.out.print("Masukkan elemen yang akan ditambahkan: "); String element = "A" + numb++; String data = queue.enqueue(element); if(data != null){ System.out.println("Antrian Anda : " + data); } else { numb--; } break; case 2: String removedElement = queue.dequeue(); if (removedElement != null) { System.out.println("Antrian " + removedElement + " Selesai Dilayani."); } String nextElement = queue.getFront(); if (nextElement != null) { System.out.println("Antrian Selanjutnya: " + nextElement); } break; case 3: String frontElement = queue.getFront(); if (frontElement != null) { System.out.println("Antrian " + frontElement + " Silakan Masuk"); } break; case 4: System.out.println("Jumlah Antrian : " + queue.getSize()); break; case 0: System.out.println("Terima Kasih!"); System.exit(0); default: System.out.println("Pilihan tidak valid. Silakan coba lagi."); break; } } } }
public class Queue { private static final int MAX_SIZE = 5; private String[] queueArray; private int front; private int rear; private int size; public Queue() { queueArray = new String[MAX_SIZE]; front = 0; rear = -1; size = 0; } public boolean isFull() { return size == MAX_SIZE; } public boolean isEmpty() { return size == 0; } public String enqueue(String data) { if (isFull()) { System.out.println("Antrian penuh. Tidak dapat menambahkan elemen " + data); return null; } rear = (rear + 1) % MAX_SIZE; queueArray[rear] = data; size++; return data; } public String dequeue() { if (isEmpty()) { System.out.println("Antrian kosong."); return null; } String removedElement = queueArray[front]; front = (front + 1) % MAX_SIZE; size--; return removedElement; } public String getFront() { if (isEmpty()) { System.out.println("Belum Ada Antrian."); return null; } return queueArray[front]; } public int getSize() { return size; } }
Editor Settings
Theme
Key bindings
Full width
Lines