0% found this document useful (0 votes)
14 views86 pages

70 Falgunidabhi PA1

Uploaded by

Falguni Dabhi
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)
14 views86 pages

70 Falgunidabhi PA1

Uploaded by

Falguni Dabhi
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/ 86

Name : Dabhi Falguni Rameshbhai

Roll No : 70
Subject : 801 java Web Development
M.sc(IT)
Sem : 8
1. Write a Java program to create an array list, add some
colors (strings) and print out the collec on.
public sta c class q1 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
}
}
Output:
2. Write a Java program to iterate through all elements in an
array list.
public sta c class q2 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
// Print the list
for (String element : list_Strings) {
System.out.println(element);
}
}
}
Output:
3. Write a Java program to insert an element into the
array list at the first posi on.
public sta c class q3 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings); list_Strings.add(0,
"Pink"); list_Strings.add(5, "Yellow");
System.out.println(list_Strings);
}
}
Output:
4. Write a Java program to retrieve an element (at a
specified index) from a given array list.
public sta c class q4 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
String element = list_Strings.get(0);
System.out.println("First element: " + element);
element = list_Strings.get(2);
System.out.println("Third element: " + element);
}
}
Output:
5. Write a Java program to update an array element by the
given element.
public sta c class q5 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings); list_Strings.set(2,
"Yellow");
System.out.println(list_Strings);
}
}
Output:

6. Write a Java program to remove the third element from


an array list.
public sta c class q6 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println(list_Strings);
list_Strings.remove(2);
System.out.println("A er removing third element from the
list:\n" + list_Strings);
}
}

Output:
7. Write a Java program to search for an element in an
array list.
public static class q7 {
public sta c void display() {
List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black"); if
(list_Strings.contains("Red")) {
System.out.println("Found the element");
} else {
System.out.println("There is no such element");
}
}
}
Output:
8. Write a Java program to sort a given array list.
public sta c class q8 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");

System.out.println("List before sort: " + list_Strings);


Collec ons.sort(list_Strings);
System.out.println("List a er sort: " + list_Strings);
} }
Output:
9. Write a Java program to copy one array list into another.
public sta c class q9 {

public sta c void display() {


List<String> List1 = new ArrayList<String>();
List1.add("1");
List1.add("2");
List1.add("3");
List1.add("4");
System.out.println("List1: " + List1);
List<String> List2 = new ArrayList<String>();
List2.add("A");
List2.add("B");
List2.add("C");
List2.add("D");
System.out.println("List2: " + List2);
Collec ons.copy(List1, List2);
System.out.println("Copy List to List2,\nA er copy:");
System.out.println("List1: " + List1);
System.out.println("List2: " + List2);
} }

Output:
10. Write a Java program to shuffle elements in an array list.
public sta c class q10 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println("List before shuffling:\n" + list_Strings);
Collec ons.shuffle(list_Strings);
System.out.println("List a er shuffling:\n" + list_Strings);
}
}
Output:

11. Write a Java program to reverse elements in an array


list.
public sta c class q11 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println("List before reversing :\n" + list_Strings);
Collec ons.reverse(list_Strings);
System.out.println("List a er reversing :\n" + list_Strings);
} }

Output:
12. Write a Java program to extract a por on of an array list.
public sta c class q12 {

public sta c void display() {


List<String> list_Strings = new ArrayList<String>();
list_Strings.add("Red"); list_Strings.add("Green");
list_Strings.add("Orange"); list_Strings.add("White");
list_Strings.add("Black");
System.out.println("Original list: " + list_Strings);
List<String> sub_List = list_Strings.subList(0, 3);
System.out.println("List of first three elements: " +
sub_List);
}
}
Output:
13. Write a Java program to compare two array lists.
public sta c class q13 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");
c1.add("Black"); c1.add("White");
c1.add("Pink");

ArrayList<String> c2 = new ArrayList<String>();


c2.add("Red"); c2.add("Green");
c2.add("Black"); c2.add("Pink");

ArrayList<String> c3 = new ArrayList<String>();


for (String e : c1) {
c3.add(c2.contains(e) ? "Yes" : "No");
}
System.out.println(c3);
}
}
Output:

14. Write a Java program that swaps two elements in an


array list.
public sta c class q14 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");

c1.add("Black");
c1.add("White"); c1.add("Pink");
System.out.println("Array list before Swap:");
for (String a : c1) {
System.out.println(a);
}
Collec ons.swap(c1, 0, 2);
System.out.println("Array list a er swap:");
for (String b : c1) {
System.out.println(b);
}
}
}
Output:
15. Write a Java program to join two array lists.
public sta c class q15 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");

c1.add("Black");
c1.add("White"); c1.add("Pink");
System.out.println("List of first array: " + c1);
ArrayList<String> c2 = new ArrayList<String>();
c2.add("Red"); c2.add("Green");
c2.add("Black"); c2.add("Pink");
System.out.println("List of second array: " + c2);

ArrayList<String> a = new ArrayList<String>();


a.addAll(c1);
a.addAll(c2);
System.out.println("New array: " + a);
}
}
Output:
16. Write a Java program to clone an array list to another
array list.
public sta c class q16 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");
c1.add("Black"); c1.add("White");
c1.add("Pink");
System.out.println("Original array list: " + c1);
ArrayList<String> newc1 = (ArrayList<String>) c1.clone();
System.out.println("Cloned array list: " + newc1);
}
}
Output:
17. Write a Java program to empty an array list.
public static class q17 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");
c1.add("Black"); c1.add("White");
c1.add("Pink");

System.out.println("Original array list: " + c1);


c1.removeAll(c1);
System.out.println("Array list a er remove all elements " +
c1);
}
}
Output:
18. Write a Java program to test whether an array list is
empty or not.
public sta c class q18 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");
c1.add("Black"); c1.add("White");
c1.add("Pink");
System.out.println("Original array list: " + c1);
System.out.println("Checking the above array list is
empty or not! " + c1.isEmpty()); c1.removeAll(c1);
System.out.println("Array list a er remove all elements " +
c1);
System.out.println("Checking the above array list is empty
or not! " + c1.isEmpty());
}
}
Output:
19. Write a Java program for trimming the capacity of an
array list.
public sta c class q19 {

public static void display() {


ArrayList<String> c1 = new ArrayList<String>();
c1.add("Red"); c1.add("Green");
c1.add("Black"); c1.add("White");
c1.add("Pink");
System.out.println("Original array list: " + c1);
System.out.println("Let trim to size the above array: ");
c1.trimToSize();
System.out.println(c1);
}
}
Output:
20. Write a Java program to increase an array list size.
public sta c class q20 {

public sta c void display() {


ArrayList<String> c1 = new ArrayList<String>(3);
c1.add("Red"); c1.add("Green");
c1.add("Black");
System.out.println("Original array list: " + c1);
c1.ensureCapacity(6); c1.add("White");
c1.add("Pink"); c1.add("Yellow");
System.out.println("New array list: " + c1);
}
}
Output:
21. Write a Java program to replace the second element of
an ArrayList with the specified element.
public sta c class q21 {

public sta c void display() {


ArrayList<String> color = new ArrayList<String>();

color.add("Red");
color.add("Green");

System.out.println("Original array list: " + color);


String new_color = "White"; color.set(1, new_color);

int num = color.size();


System.out.println("Replace second element with
'White'.");
for (int i = 0; i < num; i++) {
System.out.println(color.get(i));
}
}
}
Output:
22. Write a Java program to print all the elements of an
ArrayList using the elements.
public sta c class q22 {

public sta c void display() {


ArrayList<String> arrayList = new ArrayList<>();

// Add elements to the ArrayList


arrayList.add("Element 1");
arrayList.add("Element 2");
arrayList.add("Element 3");
arrayList.add("Element 4");
arrayList.add("Element 5"); for (int i = 0;
i < arrayList.size(); i++) {
System.out.println("Posi on " + i + ": " + arrayList.get(i));
}
}
}
Output:
B ) LinkedLists
1. Write a Java program to append the specified element
to the end of a linked list.
public sta c class qb1 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

linkedList.add("Element 1");
linkedList.add("Element 2"); linkedList.add("Element
3");

System.out.println("Original LinkedList: " + linkedList);

appendElement(linkedList, "New Element");

System.out.println("Modified LinkedList: " + linkedList);


}
private sta c void appendElement(LinkedList<String>
linkedList, String element) {
linkedList.addLast(element);
}
}
Output:

2. Write a Java program to iterate through all elements in a


linked list.
public sta c class qb2 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Element 1");
linkedList.add("Element 2"); linkedList.add("Element
3");

iterateThroughList(linkedList);
}

private sta c void iterateThroughList(LinkedList<String>


linkedList) {
System.out.println("Using for-each loop:");
for (String element : linkedList) {
System.out.println(element);
}

System.out.println("\nUsing Iterator:");
Iterator<String> iterator = linkedList.iterator(); while
(iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
}
}
Output:
3. Write a Java program to iterate through all elements in
a linked list star ng at the specified posi on.
public sta c class qb3 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

int startPosi on = 2;

iterateFromPosi on(linkedList, startPosi on);


}

private sta c void iterateFromPosi on(LinkedList<String>


linkedList, int startPosi on) {
if (startPosi on < 0 || startPosi on >= linkedList.size())
{
System.out.println("Invalid star ng posi on.");
return;
}

ListIterator<String> iterator = linkedList.listIterator(startPosi


on);

System.out.println("Elements star ng from posi on " +


startPosi on + ":");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

Output:
4. Write a Java program to iterate a linked list in reverse
order.
public sta c class qb4 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");
reverseIterate(linkedList);
}

private sta c void reverseIterate(LinkedList<String> linkedList)


{
ListIterator<String> iterator =
linkedList.listIterator(linkedList.size());

System.out.println("Elements in reverse order:");


while (iterator.hasPrevious()) {
System.out.println(iterator.previous());
}
}
}
Output:
5. Write a Java program to insert the specified element at
the specified posion in the linked list.
public sta c class qb5 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");
System.out.println("Original LinkedList: " + linkedList);

String elementToInsert = "New Element";


int posi onToInsert = 2;

insertAtPosi on(linkedList, elementToInsert, posi


onToInsert);

System.out.println("Modified LinkedList: " + linkedList);


}

private sta c void insertAtPosi on(LinkedList<String>


linkedList, String element, int posi on) { if (posi on < 0 ||
posi on > linkedList.size()) { System.out.println("Invalid
posi on for inser on."); return;
}

linkedList.add(posi on, element);


}
}
Output:

6. Write a Java program to insert elements into the


linked list at the first and last posions.
public sta c class qb6 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Element 2"); linkedList.add("Element 3");

System.out.println("Original LinkedList: " + linkedList);


insertAtFirstPosi on(linkedList, "Element 1");
insertAtLastPosi on(linkedList, "Element 4");

System.out.println("Modified LinkedList: " + linkedList);


}

private sta c void insertAtFirstPosi on(LinkedList<String>


linkedList, String element) { linkedList.addFirst(element);
}

private sta c void insertAtLastPosi on(LinkedList<String>


linkedList, String element) { linkedList.addLast(element);
}
}
Output:
7. Write a Java program to insert the specified element at
the front of a linked list.
public static class qb7 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

linkedList.add("Element 2");
linkedList.add("Element 3");

System.out.println("Original LinkedList: " + linkedList);

insertAtFront(linkedList, "Element 1");

System.out.println("Modified LinkedList: " + linkedList);


}

private sta c void insertAtFront(LinkedList<String>


linkedList, String element) {
linkedList.addFirst(element);
}
}
Output:

8. Write a Java program to insert the specified element at


the end of a linked list.
public sta c class qb8 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
// Print the original LinkedList
System.out.println("Original LinkedList: " + linkedList);

// Insert an element at the end of the LinkedList


insertAtEnd(linkedList, "New Element");

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void insertAtEnd(LinkedList<String> linkedList,


String element) {
// Use the addLast method to insert the element at the end
linkedList.addLast(element);
}
}
Output:

9. Write a Java program to insert some elements at the


specified posi on into a linked list.
public sta c class qb9 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
5");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);
// Specify the posi on and elements to insert
int posi onToInsert = 1;
String[] elementsToInsert = {"Element 2", "Element 3",
"Element 4"};

// Insert elements at the specified posi on


insertAtPosi on(linkedList, posi onToInsert,
elementsToInsert);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void insertAtPosi on(LinkedList<String>


linkedList, int posi on, String[] elements) { // Check if
the posi on is valid
if (posi on < 0 || posi on > linkedList.size()) {
System.out.println("Invalid posi on for inser on."); return;
}

// Use the addAll method to insert elements at the specified


posi on
for (int i = 0; i < elements.length; i++) {
linkedList.add(posi on + i, elements[i]);
}
}

public sta c class qb10 {

public sta c void display() {


// Create a LinkedList
LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 2"); linkedList.add("Element
4"); linkedList.add("Element 2");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Specify the element to find


String elementToFind = "Element 2";
// Find the first and last occurrences of the specified
element
findOccurrences(linkedList, elementToFind);
}

private sta c void findOccurrences(LinkedList<String>


linkedList, String element) {
// Find the first occurrence
int firstIndex = linkedList.indexOf(element);

// Find the last occurrence


int lastIndex = linkedList.lastIndexOf(element);

if (firstIndex != -1 && lastIndex != -1) {


System.out.println("First occurrence of '" + element + "'
at index: " + firstIndex);
System.out.println("Last occurrence of '" + element + "'
at index: " + lastIndex);
} else {
System.out.println("Element '" + element + "' not found
in the LinkedList.");
}
}
}

public sta c class qb11 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Display elements and their posi ons in the LinkedList


displayElementsAndPosi ons(linkedList);
}

private sta c void


displayElementsAndPosi ons(LinkedList<String> linkedList) {
// Iterate through the LinkedList using a for loop
System.out.println("Elements and their posi ons:");
for (int i = 0; i < linkedList.size(); i++) {
String element = linkedList.get(i);
System.out.println("Posi on " + i + ": " + element);
}
}

Output:
10. Write a Java program to get the first and last
occurrence of the specified elements in a linked list.
public sta c class qb10 {

public sta c void display() {


// Create a LinkedList
LinkedList<String> linkedList = new
LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 2"); linkedList.add("Element
4"); linkedList.add("Element 2");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Specify the element to find


String elementToFind = "Element 2";
// Find the first and last occurrences of the specified
element
findOccurrences(linkedList, elementToFind);
}

private sta c void findOccurrences(LinkedList<String>


linkedList, String element) {
// Find the first occurrence
int firstIndex = linkedList.indexOf(element);

// Find the last occurrence


int lastIndex = linkedList.lastIndexOf(element);

if (firstIndex != -1 && lastIndex != -1) {


System.out.println("First occurrence of '" + element + "'
at index: " + firstIndex);
System.out.println("Last occurrence of '" + element + "'
at index: " + lastIndex);
} else {
System.out.println("Element '" + element + "' not found
in the LinkedList.");
}
}
}
Output:

11. Write a Java program to display elements and


their posions in a linked list.
public sta c class qb11 {
public sta c void display() {
LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Display elements and their posi ons in the LinkedList


displayElementsAndPosi ons(linkedList);
}

private sta c void


displayElementsAndPosi ons(LinkedList<String> linkedList) {
// Iterate through the LinkedList using a for loop
System.out.println("Elements and their posi ons:");

for (int i = 0; i < linkedList.size(); i++) {


String element = linkedList.get(i);
System.out.println("Posi on " + i + ": " + element);
}
}

Output:
12. Write a Java program to remove a specified element
from a linked list.
public sta c class qb12 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);
// Specify the element to remove
String elementToRemove = "Element 3";

// Remove the specified element


removeElement(linkedList, elementToRemove);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void removeElement(LinkedList<String>


linkedList, String element) {
// Use the remove method to remove the specified element
linkedList.remove(element);
}
}
Output:

13. Write a Java program to remove the first


and last elements from a linked list.
public sta c class qb13 {
public sta c void display() {
LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Remove the first and last elements


removeFirstAndLast(linkedList);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void removeFirstAndLast(LinkedList<String>


linkedList) {
// Use the removeFirst and removeLast methods to remove
the first and last elements linkedList.removeFirst();
linkedList.removeLast();
}
}
Output:

14. Write a Java program to remove all elements from a


linked list.
public sta c class qb14 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();
// Add elements to the LinkedList
linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Remove all elements from the LinkedList


removeAllElements(linkedList);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void removeAllElements(LinkedList<String>


linkedList) {
// Use the clear method to remove all elements from the
linked list linkedList.clear();
}
}
Output:

15. Write a Java program that swaps two elements in a


linked list.
public sta c class qb15 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");
// Print the original LinkedList
System.out.println("Original LinkedList: " + linkedList);

// Specify the indices of elements to swap


int index1 = 1;
int index2 = 3;

// Swap elements at the specified indices


swapElements(linkedList, index1, index2);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void swapElements(LinkedList<String>


linkedList, int index1, int index2) { // Check if indices
are valid
if (index1 < 0 || index1 >= linkedList.size() || index2 < 0 ||
index2 >= linkedList.size()) {
System.out.println("Invalid indices for swapping.");
return;
}

// Use the set method to swap elements at the specified


indices
String temp = linkedList.get(index1);
linkedList.set(index1, linkedList.get(index2));
linkedList.set(index2, temp);
} }

Output:

16. Write a Java program to shuffle elements in a linked list.


public sta c class qb16 {
public sta c void display() {
LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
linkedList.add("Element 4"); linkedList.add("Element
5");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Shuffle elements in the LinkedList


shuffleLinkedList(linkedList);

// Print the shuffled LinkedList


System.out.println("Shuffled LinkedList: " + linkedList);
}

private sta c void shuffleLinkedList(LinkedList<String>


linkedList) {
// Use the shuffle method from the Collec ons class to shuffle
the linked list
Collec ons.shuffle(linkedList);
}
}
Output:

17. Write a Java program to join two linked lists.


public sta c class qb17 {

public sta c void display() {


LinkedList<String> list1 = new LinkedList<>();
list1.add("Element 1"); list1.add("Element 2");
list1.add("Element 3");

// Create the second LinkedList


LinkedList<String> list2 = new LinkedList<>();
list2.add("Element 4"); list2.add("Element 5");
list2.add("Element 6");

// Print the original LinkedLists


System.out.println("Original LinkedList 1: " + list1);
System.out.println("Original LinkedList 2: " + list2);

// Join the LinkedLists


LinkedList<String> joinedList = joinLinkedLists(list1, list2);

// Print the joined LinkedList


System.out.println("Joined LinkedList: " + joinedList);
}

private sta c LinkedList<String>


joinLinkedLists(LinkedList<String> list1, LinkedList<String> list2)
{
// Create a new LinkedList to store the joined result
LinkedList<String> joinedList = new LinkedList<>();

// Add all elements from the first list


joinedList.addAll(list1);

// Add all elements from the second list


joinedList.addAll(list2);

return joinedList;
}
}
Output:
18. Write a Java program to copy a linked list to another
linked list.
public sta c class qb18 {

public sta c void display() {


LinkedList<String> originalList = new LinkedList<>();
originalList.add("Element 1"); originalList.add("Element
2"); originalList.add("Element 3");

// Print the original LinkedList


System.out.println("Original LinkedList: " + originalList);

// Copy the original LinkedList to a new LinkedList


LinkedList<String> copiedList =
copyLinkedList(originalList);

// Print the copied LinkedList


System.out.println("Copied LinkedList: " + copiedList);
}

private sta c LinkedList<String>


copyLinkedList(LinkedList<String> originalList) {
// Create a new LinkedList to store the copied elements
LinkedList<String> copiedList = new LinkedList<>();

// Use the addAll method to copy elements from the


original list to the new list
copiedList.addAll(originalList);

return copiedList;
}
}
Output:

19. Write a Java program to remove and return the


first element of a linked list.
public sta c class qb19 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Remove and return the first element


String removedElement = removeFirstElement(linkedList);

// Print the modified LinkedList and the removed element


System.out.println("Modified LinkedList: " + linkedList);
System.out.println("Removed Element: " +
removedElement);
}

private sta c String


removeFirstElement(LinkedList<String> linkedList) {
// Use the removeFirst method to remove and return the first
element
return linkedList.pollFirst();
}
}
Output:

20. Write a Java program to retrieve, but not remove, the


first element of a linked list.
public sta c class qb20 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Retrieve, but not remove, the first element


String firstElement = retrieveFirstElement(linkedList);

// Print the original LinkedList and the retrieved element


System.out.println("Original LinkedList: " + linkedList);
System.out.println("Retrieved Element: " + firstElement);
}

private sta c String


retrieveFirstElement(LinkedList<String> linkedList) {
// Use the getFirst method to retrieve, but not remove, the
first element
return linkedList.getFirst();
}
}
Output:

21. Write a Java program to retrieve, but not remove, the


last element of a linked list.
public sta c class qb21 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();
// Add elements to the LinkedList
linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);
// Retrieve, but not remove, the last element
String lastElement = retrieveLastElement(linkedList);

// Print the original LinkedList and the retrieved element


System.out.println("Original LinkedList: " + linkedList);
System.out.println("Retrieved Last Element: " +
lastElement);
}

private sta c String


retrieveLastElement(LinkedList<String> linkedList) {
// Use the getLast method to retrieve, but not remove, the last
element
return linkedList.getLast();
}
}
Output:

22. Write a Java program to check if a par cular element


exists in a linked list.
public sta c class qb22 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
// Print the original LinkedList
System.out.println("Original LinkedList: " + linkedList);

// Check if a par cular element exists in the LinkedList


String elementToCheck = "Element 2"; boolean exists =
checkElementExists(linkedList, elementToCheck);

// Print the result


System.out.println("Does '" + elementToCheck + "' exist in
the LinkedList? " + exists);
}

private sta c boolean


checkElementExists(LinkedList<String> linkedList, String element)
{
// Use the contains method to check if the element exists in
the linked list
return linkedList.contains(element);
}
}
Output:

23. Write a Java program to convert a linked list to an array


list.
public sta c class qb23 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");

// Print the original LinkedList


System.out.println("Original LinkedList: " + linkedList);

// Convert the LinkedList to an ArrayList


ArrayList<String> arrayList =
convertToArrayList(linkedList);

// Print the converted ArrayList


System.out.println("Converted ArrayList: " + arrayList);
}

private sta c ArrayList<String>


convertToArrayList(LinkedList<String> linkedList) {
// Create a new ArrayList and use the addAll method to copy
elements from the linked list
ArrayList<String> arrayList = new
ArrayList<>(linkedList); return
arrayList;
} }
Output:

24. Write a Java program to compare two linked lists.


public sta c class qb24 {

public sta c void display() {


LinkedList<String> list1 = new LinkedList<>();
list1.add("Element 1"); list1.add("Element 2");
list1.add("Element 3");

// Create the second LinkedList


LinkedList<String> list2 = new LinkedList<>();
list2.add("Element 1"); list2.add("Element 2");
list2.add("Element 3");
// Print the original LinkedLists
System.out.println("LinkedList 1: " + list1);
System.out.println("LinkedList 2: " + list2);

// Compare the LinkedLists


boolean isEqual = compareLinkedLists(list1, list2);

// Print the result


System.out.println("Are the LinkedLists equal? " + isEqual);
}

private sta c boolean


compareLinkedLists(LinkedList<String> list1, LinkedList<String>
list2) {
// Use the equals method to compare the en re contents of the
linked lists
return list1.equals(list2);
}
}
Output:

25. Write a Java program to check if a linked list is empty or


not.
public sta c class qb25 {

public sta c void display() {


LinkedList<String> emptyList = new LinkedList<>();

// Create a non-empty LinkedList


LinkedList<String> nonEmptyList = new LinkedList<>();
nonEmptyList.add("Element 1");
nonEmptyList.add("Element 2");
// Check if the linked lists are empty
boolean isEmptyEmptyList = checkIfEmpty(emptyList);
boolean isEmptyNonEmptyList = checkIfEmpty(nonEmptyList);

// Print the results


System.out.println("Is the empty list empty? " +
isEmptyEmptyList);
System.out.println("Is the non-empty list empty? " +
isEmptyNonEmptyList);
}

private sta c boolean checkIfEmpty(LinkedList<String>


linkedList) {
// Use the isEmpty method to check if the linked list is empty
return linkedList.isEmpty();
}
}
Output:

26. Write a Java program to replace an element in a linked


list.
public sta c class qb26 {

public sta c void display() {


LinkedList<String> linkedList = new LinkedList<>();

// Add elements to the LinkedList


linkedList.add("Element 1"); linkedList.add("Element
2"); linkedList.add("Element 3");
// Print the original LinkedList
System.out.println("Original LinkedList: " + linkedList);

// Replace an element in the LinkedList


String oldElement = "Element 2"; String
newElement = "New Element 2";
replaceElement(linkedList, oldElement, newElement);

// Print the modified LinkedList


System.out.println("Modified LinkedList: " + linkedList);
}

private sta c void replaceElement(LinkedList<String>


linkedList, String oldElement, String newElement) {
// Use the indexOf and set methods to replace the old
element with the new element
int index = linkedList.indexOf(oldElement);

if (index != -1) {
linkedList.set(index, newElement);
System.out.println("Element replaced successfully.");
} else {
System.out.println("Element not found in the
LinkedList.");
}
}
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy