0% found this document useful (0 votes)
11 views5 pages

Skill Week - 6 (Java Generics, List, Queue - 27-01-2025 To 01-02-2025)

The document outlines the creation of a custom generic data structure named ListQueue that integrates List and Queue functionalities, focusing on efficient performance for both random access and queue operations. It also describes the implementation of a type-safe PriorityQueue that orders elements based on their priority, ensuring correct element ordering and efficient priority management. The document includes code examples demonstrating the operations of both data structures.

Uploaded by

hope98754
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)
11 views5 pages

Skill Week - 6 (Java Generics, List, Queue - 27-01-2025 To 01-02-2025)

The document outlines the creation of a custom generic data structure named ListQueue that integrates List and Queue functionalities, focusing on efficient performance for both random access and queue operations. It also describes the implementation of a type-safe PriorityQueue that orders elements based on their priority, ensuring correct element ordering and efficient priority management. The document includes code examples demonstrating the operations of both data structures.

Uploaded by

hope98754
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/ 5

Name: VEMURU VENISHA

Roll no:2300032694
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<T> 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<T> 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.

Answer
package week6skill;
//Skill Week 6 First Question
import java.util.ArrayList;

public class firststquestion


{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
list.add("Ravi");
list.add("Vijay");
list.add("Ravi");
list.remove("Ravi");
for(String str:list)
{
System.out.println(str);
}
System.out.println(list.get(0));
list.set(1, "Raj");
System.out.println("Updated list:");
for (String str : list)
{
System.out.println(str);
}
}
}
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<T> where T must
implement Comparable<T>.
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<T> 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.

Answer
package HashSet;
import java.util.*;
public class PriorityQueueDemo
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
// Add elements to the queue
void enqueue()
{
pq.add(10);
pq.add(20);
pq.add(30);
pq.add(40);
System.out.println("PriorityQueue: " + pq);
}
void dequeue()
{
// Remove and print elements in priority order
System.out.print("Elements in priority order: ");
System.out.print(pq.peek() + " ");

}
public static void main(String args[])
{
PriorityQueueDemo m=new PriorityQueueDemo();
m.enqueue();
m.dequeue();
}
}

You might also like