0% found this document useful (0 votes)
75 views6 pages

Laporan Praktikum Queue

This document discusses different types of queues in Java including array queues, priority queues, and priority queues for products. It provides code examples for implementing each type of queue and sample output. Array queues are implemented using a circular array. Priority queues sort elements based on their natural ordering or a custom comparator. The product priority queue example sorts products based on a priority enum field.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views6 pages

Laporan Praktikum Queue

This document discusses different types of queues in Java including array queues, priority queues, and priority queues for products. It provides code examples for implementing each type of queue and sample output. Array queues are implemented using a circular array. Priority queues sort elements based on their natural ordering or a custom comparator. The product priority queue example sorts products based on a priority enum field.

Uploaded by

Rafiq Muzakki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Algoritma dan Struktur Data

Resmi

Praktikum Queue
-

Praditya Nafiis Muhammad

2 D3 IT B

2103181032

Yuliana Setyowati

23 November 2019
A. Queue dengan Array
 Listing Program
package queue;

public interface Queue<T> {

public abstract boolean isEmpty();

public T peek();

public T pop();

public void push(T item);

public int size();


}

package queue;

public interface BQueue<T> extends Queue<T> {

boolean full();
}

package queue;

import java.util.Arrays;

public class ArrQueue<T> implements BQueue<T> {

private T Arr[];
private int qfront = 0;
private int qback = 0;
private int qcapacity = 0;
private int size;

public ArrQueue() {
Arr = (T[]) new Object[5];
qcapacity = 5;
}

public ArrQueue(int size) {


Arr = (T[]) new Object[size];
qcapacity = size;
}

@Override
public boolean full() {
return size == qcapacity;
}

@Override
public boolean isEmpty() {
return (size == 0);
}

@Override
public T peek() {
return Arr[qfront];
}

@Override
public T pop() {
T temp = Arr[qfront];
Arr[qfront] = null;
qfront = (qfront + 1) % qcapacity;
size--;
return temp;

@Override
public void push(T item) {
if (size < qcapacity) {
Arr[qback] = item;
qback = (qback + 1) % qcapacity;
size++;
} else {
System.out.println("Queue Penuh!");
}

@Override
public int size() {
return size;
}

@Override
public String toString() {
return "ArrQueue{" + "Arr=" + Arrays.toString(Arr) + ", qfront=" +
qfront + ", qback=" + qback + ", qcapacity=" + qcapacity + ", size=" + size
+ '}';
}
}

package queue;

import java.util.NoSuchElementException;
public class ArrQueueTest {

public static void main(String[] args) {


try {
ArrQueue<Integer> arr = new ArrQueue<Integer>(5);
arr.push(1);
arr.push(4);
arr.push(10);
arr.push(2);
arr.push(7);
System.out.println(arr);
arr.pop();
arr.pop();
System.out.println(arr);
arr.push(17);
arr.push(3);
arr.push(27);
System.out.println(arr);
arr.push(61);
System.out.println(arr);
} catch (NoSuchElementException e) {
System.out.println(e.getMessage());
}
}
}

 Output Program
B. Priority Queue
 Listing Program
package PriorityQueue;

import java.util.*;

public class PriorityQueueDemo {

public static void main(String[] args) {


PriorityQueue<String> stringQueue;
stringQueue = new PriorityQueue<String>();
stringQueue.add("ab");
stringQueue.add("abcd");
stringQueue.add("abc");
stringQueue.add("a");

while (stringQueue.size() > 0) {


System.out.println(stringQueue.remove());
}
}
}

 Output Program
C. Priority Queue Produk
 Listing Program
package PriorityQueue;

import java.util.PriorityQueue;

public class Main {

public static void main(String args[]) {


PriorityQueue<Product> pq = new PriorityQueue<Product>(3);
pq.add(new Product("A", ProductQuality.Low));
pq.add(new Product("B", ProductQuality.High));
pq.add(new Product("C", ProductQuality.Medium));
Product m;
while ((m = pq.poll()) != null) {
System.out.println(m.name + " Priority: " + m.priority);
}
PriorityQueue<Product> pqRev = new PriorityQueue<Product>(3, new
MessageComparator());
pqRev.add(new Product("D", ProductQuality.Low));
pqRev.add(new Product("E", ProductQuality.High));
pqRev.add(new Product("F", ProductQuality.Medium));
while ((m = pqRev.poll()) != null) {
System.out.println(m.name + " Priority: " + m.priority);
}
}
}

 Output Program

You might also like