1.
Sample Exercise to print the list of items using Arraylist
PROGRAM CODING:
import [Link];
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> fruits = new ArrayList<String>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Fruits: " + fruits);
[Link]("Second fruit: " + [Link](1));
[Link](0, "Apricot");
[Link]("Cherry");
[Link]("Updated fruits: " + fruits);
}
}
2. Sample Exercise to print the elements using linked list
PROGRAM CODING:
import [Link];
public class LinkedListExample
{
public static void main(String[] args)
{
LinkedList<Integer> numbers = new LinkedList<Integer>();
[Link](10);
[Link](20);
[Link](30);
[Link]("Numbers: " + numbers);
[Link](5);
[Link](40);
[Link]("Updated numbers: " + numbers);
[Link]();
[Link]("Final numbers: " + numbers);
}
}
3. Sample Exercise for implementation of stack
PROGRAM CODING:
public class StackExample
{
public static void main(String[] args)
{
Stack<String> bookStack = new Stack<String>();
// Add books to the stack
[Link]("Book 1");
[Link]("Book 2");
[Link]("Book 3");
[Link]("Stack size: " + [Link]());
[Link]("Top book: " + [Link]());
// Remove the top book
String removedBook = [Link]();
[Link]("Removed book: " + removedBook);
[Link]("New stack size: " + [Link]());
[Link]("New top book: " + [Link]());
}
}
4. Sample Exercise for implementation of Linked List
PROGRAM CODIND:
import [Link];
public class samplelist
{
public static void main(String[] args)
{
LinkedList<String> list1 = new LinkedList<String>();
[Link]("100");
[Link]("200");
[Link]("300");
[Link]("400");
[Link]("500");
[Link]("First element of LinkedList is : " + [Link]());
[Link]("Last element of LinkedList is : " + [Link]());
}
}