0% found this document useful (0 votes)
31 views

Tugas Sda Membuat Kodingan Queue: 1. Screen Shoot Program Queue

The document describes a queue coding program in Java. It includes the source code to create a queue using a linked list, add 10 string elements to the queue, and demonstrate FIFO behavior by printing the queue vertically and horizontally, peeking at the head element, removing elements from the head and tail of the queue, and finally removing all elements from the queue. The output screenshots show the queue displaying its elements as intended.

Uploaded by

Guman Iradi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Tugas Sda Membuat Kodingan Queue: 1. Screen Shoot Program Queue

The document describes a queue coding program in Java. It includes the source code to create a queue using a linked list, add 10 string elements to the queue, and demonstrate FIFO behavior by printing the queue vertically and horizontally, peeking at the head element, removing elements from the head and tail of the queue, and finally removing all elements from the queue. The output screenshots show the queue displaying its elements as intended.

Uploaded by

Guman Iradi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

TUGAS SDA MEMBUAT KODINGAN QUEUE

1. Screen Shoot Program Queue

G1A017052
2. Source Code
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class queue1
{
static void printQueueVertically(Queue queue)
{
System.out.println("-----------------------");
Iterator<String> i = queue.iterator();
while (i.hasNext()) {
String s = i.next();
System.out.println (s);
}
System.out.println("----------------------");
}
static void printQueueHorizontally(Queue queue)
{
if(!queue.isEmpty()) {
System.out.println("");
System.out.println("Queue elements : " +queue);
System.out.println("");
}
else {
System.out.println("");
System.out.println("Queue is empty!");
System.out.println("");
}
}
public static void main (String[]args) {
Queue<String>queue = new LinkedList<String>(); // Membuat Program QUeue
// Menambah elemen ke dalam queue
System.out.println("Program Antrian (Queue)");
System.out.println("Disusun Oleh Guman Iradi");
System.out.println("NPM : G1A017052");
queue.add("Guman");
queue.add("Yogi");
queue.add("Nanda");
queue.add("Teguh");
queue.add("Hastri");
queue.add("Safitri");
queue.add("Rozaaq");
queue.add("Zendi");
queue.add("Ikhwan");
queue.add("Toby");

System.out.println("Daftar element(s) vertically :");


printQueueVertically(queue); //Mencetak Elemen Queue
printQueueHorizontally(queue);
System.out.println("The queue contain "+queue.size() + "elements");
//Mengembalikan item ke Kepala (atas) dengan ada item terhapus
System.out.println("The head element (peek) : " + queue.peek());
System.out.println("");
//Menghapus Element Terbawah dari Queue
System.out.println("Delete (poll) element : " + queue.poll());
System.out.print("DaftarElement(s) setelah pool : ");
printQueueHorizontally(queue);
System.out.println("Delete (remove) element : " + queue.remove());
System.out.print("DaftarElement(s) setelah remove :");
printQueueHorizontally(queue);

System.out.print("Delete semua elements : " + queue.removeAll(queue));


printQueueHorizontally(queue);
System.out.println("Queue: " + queue);
}
}
3. Screen Shoot Output

G1A017052

4. Penjelasan Program
Pada program queue ini, penulis memasukkan 10 buah data element menggunakan
queue. Lalu data akan diurutkan sesuai dengan yang pertama kali mendaftar. Konsep
queue adalah FIFO (First In First Out). Jadi yang pertama kali mengantri, maka akan
keluar pertama. Nah di output program sudah terdapat 10 buah element, dan digunakan
peek yang mana akan mengecek data element teratas (pertama) yaitu Guman. Lalu setelah
itu menggunakan pool dimana akan menghapus data yang paling pertama (teratas) yaitu
element Guman. Maka queue nya yang tersisa tinggal 9 element. Lalu berikutnya element
yang didelete atau diremove adalah element Yogi, yang mana setelah didelete element
yang tersisa tinggal 8 buah element. Berikutnya akan menghapus seluruh data element
antrian (queue) dengan menggunakan queue.removeAll maka semua data di queue
terhapus dan data di queue menjadi kosong (IsEmpty).

You might also like