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

Skill Week 6

The document contains two Java implementations of queue data structures: a ListQueue and a PriorityQueue. The ListQueue allows adding, removing, and updating elements, while the PriorityQueue maintains order based on element priority. Both implementations include methods for checking size and emptiness, and demonstrate their functionality through main methods with example outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Skill Week 6

The document contains two Java implementations of queue data structures: a ListQueue and a PriorityQueue. The ListQueue allows adding, removing, and updating elements, while the PriorityQueue maintains order based on element priority. Both implementations include methods for checking size and emptiness, and demonstrate their functionality through main methods with example outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Advanced Object Oriented Programming-AOOP(A)

Course Code-23CS2103A
Name: K. Madhu mitha
ID Number: 2300090088

Skill-Week-3

Question-1:
package Problem1;

import java.util.ArrayList;

import java.util.List;

class ListQueue<T> {

private List<T> elements;

public ListQueue() {

elements = new ArrayList<>();

public void add(T element) {

elements.add(element);

public T remove() {

if (elements.isEmpty()) {

throw new IllegalStateException("Queue is empty.");

return elements.remove(0);

}
public T get(int index) {

if (index < 0 || index >= elements.size()) {

throw new IndexOutOfBoundsException("Index out of bounds.");

return elements.get(index);

public void set(int index, T element) {

if (index < 0 || index >= elements.size()) {

throw new IndexOutOfBoundsException("Index out of bounds.");

elements.set(index, element);

public int size() {

return elements.size();

public boolean isEmpty() {

return elements.isEmpty();

public class Main {

public static void main(String[] args) {

ListQueue<String> listQueue = new ListQueue<>();

listQueue.add("A");

listQueue.add("B");

listQueue.add("C");
System.out.println("Initial Elements:");

for (int i = 0; i < listQueue.size(); i++) {

System.out.println(listQueue.get(i));

System.out.println("Removed: " + listQueue.remove());

listQueue.set(0, "D");

System.out.println("After Update:");

for (int i = 0; i < listQueue.size(); i++) {

System.out.println(listQueue.get(i));

System.out.println("Size: " + listQueue.size());

System.out.println("Is Empty: " + listQueue.isEmpty());

Output:

Question-2:
package Problem2;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.NoSuchElementException;

class PriorityQueue<T extends Comparable<T>> {

private List<T> elements;

public PriorityQueue() {

elements = new ArrayList<>();

public void enqueue(T element) {

elements.add(element);

Collections.sort(elements);

public T dequeue() {

if (elements.isEmpty()) {

throw new NoSuchElementException("Queue is empty.");

return elements.remove(0);

public T peek() {

if (elements.isEmpty()) {

throw new NoSuchElementException("Queue is empty.");

return elements.get(0);

}
public int size() {

return elements.size();

public boolean isEmpty() {

return elements.isEmpty();

public class Main {

public static void main(String[] args) {

PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

priorityQueue.enqueue(5);

priorityQueue.enqueue(2);

priorityQueue.enqueue(8);

priorityQueue.enqueue(1);

System.out.println("Peek: " + priorityQueue.peek());

while (!priorityQueue.isEmpty()) {

System.out.println("Dequeued: " + priorityQueue.dequeue());

try {

priorityQueue.dequeue();

} catch (NoSuchElementException e) {

System.out.println(e.getMessage());

}
}

Output:

You might also like