BDA_Experiment-1
1. Implement the following Data structures in Java
a) Linked Lists
b) Stacks
c) Queues
d) Set – HashSet & TreeSet
e) Map—HashMap & TreeMap
**********************************************************************************
a) Linked Lists
import [Link];
public class LinkedListDemo
public static void main(String[] args)
// Create a LinkedList using Java's Collection Framework
LinkedList<Integer> list = new LinkedList<>();
// Add elements to the list
[Link](10);
[Link](20);
[Link](30);
// Display the list
[Link]("Linked List: " + list);
// Remove an element
[Link]("\nRemoving 20 from the list:");
[Link]([Link](20)); // Removes the first occurrence of 20
[Link]("Updated List: " + list);
// Try removing an element not in the list
[Link]("\nRemoving 40 from the list:");
if ())
[Link]("40 not found in the list.");
[Link]("Final List: " + list);
**********************************************************************************
b) Stacks
import [Link];
public class StackDemo
public static void main(String[] args)
// Create a Stack using Java's Collection Framework
Stack<Integer> stack = new Stack<>();
// Push elements onto the stack
[Link](10);
[Link](20);
[Link](30);
// Display the stack
[Link]("Stack: " + stack);
// Pop an element from the stack
[Link]("\nPopping an element from the stack: " + [Link]());
[Link]("Updated Stack: " + stack);
// Peek at the top element of the stack
[Link]("\nPeeking at the top element: " + [Link]());
// Check if the stack is empty
[Link]("\nIs the stack empty? " + [Link]());
// Search for an element in the stack
[Link]("\nPosition of 10 in the stack: " + [Link](10)); // Returns 1-
// based position
**********************************************************************************
c) Queues
import [Link];
import [Link];
public class QueueDemo
public static void main(String[] args)
// Create a Queue using Java's Collection Framework
Queue<Integer> queue = new LinkedList<>();
// Add elements to the queue
[Link](10);
[Link](20);
[Link](30);
// Display the queue
[Link]("Queue: " + queue);
// Remove an element from the queue
[Link]("\nRemoving an element from the queue: " + [Link]());
[Link]("Updated Queue: " + queue);
// Peek at the front element of the queue
[Link]("\nPeeking at the front element: " + [Link]());
// Check if the queue is empty
[Link]("\nIs the queue empty? " + [Link]());
// Check the size of the queue
[Link]("\nSize of the queue: " + [Link]());
**********************************************************************************
d) Set – HashSet & TreeSet
HashSet
import [Link];
import [Link];
class HashSetDemo
public static void main(String[] args)
// Creating a set using the HashSet class
Set<Integer> set1 = new HashSet<>();
// Add elements to the set1
[Link](2);
[Link](3);
[Link]("Set1: " + set1);
// Creating another set using the HashSet class
Set<Integer> set2 = new HashSet<>();
// Add elements
[Link](1);
[Link](2);
[Link]("Set2: " + set2);
// Union of two sets
[Link](set1);
[Link]("Union is: " + set2);
**********************************************************************************
TreeSet
import [Link];
import [Link];
import [Link];
class TreeSetDemo
public static void main(String[] args)
// Creating a set using the TreeSet class
Set<Integer> numbers = new TreeSet<>();
// Add elements to the set
[Link](2);
[Link](3);
[Link](1);
[Link]("Set using TreeSet: " + numbers);
// Access Elements using iterator()
[Link]("Accessing elements using iterator(): ");
Iterator<Integer> iterate = [Link]();
while([Link]()) {
[Link]([Link]());
[Link](", ");
**********************************************************************************
e) Map—HashMap & TreeMap
HashMap
import [Link];
import [Link];
class HashMapDemo
public static void main(String[] args)
// Creating a map using the HashMap
Map<String, Integer> numbers = new HashMap<>();
// Insert elements to the map
[Link]("One", 1);
[Link]("Two", 2);
[Link]("Map: " + numbers);
// Access keys of the map
[Link]("Keys: " + [Link]());
// Access values of the map
[Link]("Values: " + [Link]());
// Access entries of the map
[Link]("Entries: " + [Link]());
// Remove Elements from the map
int value = [Link]("Two");
[Link]("Removed Value: " + value);
}
**********************************************************************************
TreeMap
import [Link];
import [Link];
class TreeMapDemo
public static void main(String[] args)
// Creating Map using TreeMap
Map<String, Integer> values = new TreeMap<>();
// Insert elements to map
[Link]("Second", 2);
[Link]("First", 1);
[Link]("Map using TreeMap: " + values);
// Replacing the values
[Link]("First", 11);
[Link]("Second", 22);
[Link]("New Map: " + values);
// Remove elements from the map
int removedValue = [Link]("First");
[Link]("Removed Value: " + removedValue);