0% found this document useful (0 votes)
10 views

Experiment-1

Uploaded by

spoorthiitchanti
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)
10 views

Experiment-1

Uploaded by

spoorthiitchanti
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/ 7

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 java.util.LinkedList;

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

list.add(10);

list.add(20);

list.add(30);

// Display the list

System.out.println("Linked List: " + list);

// Remove an element

System.out.println("\nRemoving 20 from the list:");

list.remove(Integer.valueOf(20)); // Removes the first occurrence of 20

System.out.println("Updated List: " + list);


// Try removing an element not in the list

System.out.println("\nRemoving 40 from the list:");

if (!list.remove(Integer.valueOf(40)))

System.out.println("40 not found in the list.");

System.out.println("Final List: " + list);

**********************************************************************************

b) Stacks

import java.util.Stack;

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

stack.push(10);

stack.push(20);

stack.push(30);

// Display the stack

System.out.println("Stack: " + stack);

// Pop an element from the stack

System.out.println("\nPopping an element from the stack: " + stack.pop());

System.out.println("Updated Stack: " + stack);


// Peek at the top element of the stack

System.out.println("\nPeeking at the top element: " + stack.peek());

// Check if the stack is empty

System.out.println("\nIs the stack empty? " + stack.isEmpty());

// Search for an element in the stack

System.out.println("\nPosition of 10 in the stack: " + stack.search(10)); // Returns 1-


// based position

**********************************************************************************

c) Queues

import java.util.LinkedList;

import java.util.Queue;

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

queue.add(10);

queue.add(20);

queue.add(30);

// Display the queue

System.out.println("Queue: " + queue);

// Remove an element from the queue

System.out.println("\nRemoving an element from the queue: " + queue.remove());


System.out.println("Updated Queue: " + queue);

// Peek at the front element of the queue

System.out.println("\nPeeking at the front element: " + queue.peek());

// Check if the queue is empty

System.out.println("\nIs the queue empty? " + queue.isEmpty());

// Check the size of the queue

System.out.println("\nSize of the queue: " + queue.size());

**********************************************************************************

d) Set – HashSet & TreeSet

HashSet

import java.util.Set;

import java.util.HashSet;

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

set1.add(2);

set1.add(3);

System.out.println("Set1: " + set1);

// Creating another set using the HashSet class

Set<Integer> set2 = new HashSet<>();

// Add elements

set2.add(1);
set2.add(2);

System.out.println("Set2: " + set2);

// Union of two sets

set2.addAll(set1);

System.out.println("Union is: " + set2);

**********************************************************************************

TreeSet

import java.util.Set;

import java.util.TreeSet;

import java.util.Iterator;

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

numbers.add(2);

numbers.add(3);

numbers.add(1);

System.out.println("Set using TreeSet: " + numbers);

// Access Elements using iterator()

System.out.print("Accessing elements using iterator(): ");

Iterator<Integer> iterate = numbers.iterator();

while(iterate.hasNext()) {

System.out.print(iterate.next());
System.out.print(", ");

**********************************************************************************

e) Map—HashMap & TreeMap

HashMap

import java.util.Map;

import java.util.HashMap;

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

numbers.put("One", 1);

numbers.put("Two", 2);

System.out.println("Map: " + numbers);

// Access keys of the map

System.out.println("Keys: " + numbers.keySet());

// Access values of the map

System.out.println("Values: " + numbers.values());

// Access entries of the map

System.out.println("Entries: " + numbers.entrySet());

// Remove Elements from the map

int value = numbers.remove("Two");

System.out.println("Removed Value: " + value);


}

**********************************************************************************

TreeMap

import java.util.Map;

import java.util.TreeMap;

class TreeMapDemo

public static void main(String[] args)

// Creating Map using TreeMap

Map<String, Integer> values = new TreeMap<>();

// Insert elements to map

values.put("Second", 2);

values.put("First", 1);

System.out.println("Map using TreeMap: " + values);

// Replacing the values

values.replace("First", 11);

values.replace("Second", 22);

System.out.println("New Map: " + values);

// Remove elements from the map

int removedValue = values.remove("First");

System.out.println("Removed Value: " + removedValue);

You might also like