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

Skill5 32536 PDF

Uploaded by

g98pfyb97m
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)
30 views6 pages

Skill5 32536 PDF

Uploaded by

g98pfyb97m
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

Week 5.

Collection Framework Java Generics, List &


Queue
1) Custom Generic Data Structure with List and Queue Integration Objective: Create a data
structure that combines the features of both List and Queue and focus on balancing the
operations and ensuring efficient performance for both random access and queue
operations. Details:

a) Class Design: Implement a class ListQueue that combines both List and Queue
functionalities.
b) Operations:
 add(T element): Adds an element to the end of the queue (similar to enqueue
in a Queue).
 remove(): Removes and returns the element from the front of the queue
(similar to dequeue in a Queue).
 get(int index): Retrieves the element at the specified index (similar to get in a
List).
 set(int index, T element): Replaces the element at the specified index (similar
to set in a List).
c) Internal Storage: Use an internal List to manage elements. Implement logic to support
both Queue and List operations.

d) Analyze and address the following challenging points:  Balancing the operations of List
and Queue within a single class.  Ensuring efficient performance for both random access
and queue operations.

package skillweek5;
import java.util.ArrayList;
import java.util.List;
public class ListQueue<T> {
private List<T> internalList;

public ListQueue() {
internalList = new ArrayList<>();
}
public void add(T element) {
internalList.add(element);
}
public T remove() {
if (internalList.isEmpty()) {
throw new IllegalStateException("Queue is empty");

}
return internalList.remove(0);
}
public T get(int index) {
if (index < 0 || index >= internalList.size()) {

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


}
return internalList.get(index);
}
public void set(int index, T element) {

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


throw new IndexOutOfBoundsException("Index out of bounds");
}
internalList.set(index, element);
}

public int size() {


return internalList.size();
}
public boolean isEmpty() {
return internalList.isEmpty();

}
public String toString() {
return internalList.toString();
}
public static void main(String[] args) {
ListQueue<Integer> listQueue = new ListQueue<>();
listQueue.add(1);
listQueue.add(2);
listQueue.add(3);

System.out.println("ListQueue after adding elements: " + listQueue);


System.out.println("Element at index 1: " + listQueue.get(1));
listQueue.set(1, 20);
System.out.println("ListQueue after setting element at index 1: " + listQueue);
System.out.println("Removed element: " + listQueue.remove());

System.out.println("ListQueue after removing element: " + listQueue);


System.out.println("Size of ListQueue: " + listQueue.size());
System.out.println("Is ListQueue empty? " + listQueue.isEmpty());
}
}

OUTPUT:
2) Type-safe Queue with Priorities Objective: Create a priority queue that orders elements
based on their priority and ensure correct element ordering. Details:
a) Class Design: Implement a generic class PriorityQueue where T must implement
Comparable.
b) Operations:
 enqueue(T element): Adds an element to the queue, maintaining the order based
on the natural ordering of T.
 dequeue(): Removes and returns the highest-priority element (the smallest
element according to its Comparable implementation). If the queue is empty,
throw a NoSuchElementException.
 peek(): Returns the highest-priority element without removing it. If the queue is
empty, throw a NoSuchElementException.
c) Internal Storage: Use a List to store elements. Maintain the order by sorting the list or
using a priority queue mechanism.

d) Analyze and address the following challenging points:  Implementing efficient priority
management.  Ensuring elements are added and removed according to their priority.

package skillweek5;

import java.util.PriorityQueue;
import java.util.NoSuchElementException;
public class MyPriorityQueue<T extends Comparable<T>> {
private PriorityQueue<T> queue;
public MyPriorityQueue() {

queue = new PriorityQueue<>();


}
public void enqueue(T element) {
queue.add(element);
}

public T dequeue() {
if (queue.isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return queue.poll();
}
public T peek() {
if (queue.isEmpty()) {

throw new NoSuchElementException("Queue is empty");


}
return queue.peek();
}
public boolean isEmpty() {

return queue.isEmpty();
}
public int size() {
return queue.size();
}

public String toString() {


return queue.toString();
}
public static void main(String[] args) {
MyPriorityQueue<Integer> priorityQueue = new MyPriorityQueue<>();

priorityQueue.enqueue(5);
priorityQueue.enqueue(1);
priorityQueue.enqueue(3);
System.out.println("PriorityQueue after adding elements: " + priorityQueue);
System.out.println("Peek at highest-priority element: " + priorityQueue.peek());

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


System.out.println("PriorityQueue after removing an element: " + priorityQueue);
System.out.println("Size of PriorityQueue: " + priorityQueue.size());
System.out.println("Is PriorityQueue empty? " + priorityQueue.isEmpty());
}
}
OUTPUT:

You might also like