0% found this document useful (0 votes)
5 views4 pages

Sample Exercise For Data Structures

The document provides sample Java programs demonstrating the use of ArrayList, LinkedList, and Stack data structures. It includes exercises for printing and updating lists of fruits and numbers, as well as implementing a stack for books. Each code snippet illustrates basic operations such as adding, removing, and accessing elements in these data structures.

Uploaded by

skamalraj2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Sample Exercise For Data Structures

The document provides sample Java programs demonstrating the use of ArrayList, LinkedList, and Stack data structures. It includes exercises for printing and updating lists of fruits and numbers, as well as implementing a stack for books. Each code snippet illustrates basic operations such as adding, removing, and accessing elements in these data structures.

Uploaded by

skamalraj2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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]());
}
}

You might also like