-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListOperations.java
32 lines (27 loc) · 1.05 KB
/
LinkedListOperations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.LinkedList;
public class LinkedListOperations {
public static void main(String[] args) {
// Create a LinkedList
LinkedList<String> linkedList = new LinkedList<>();
// Add elements to the LinkedList
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Orange");
linkedList.add("Mango");
// Search for an element
String searchElement = "Banana";
int index = linkedList.indexOf(searchElement);
if (index != -1) {
System.out.println("Element '" + searchElement + "' found at index: " + index);
} else {
System.out.println("Element '" + searchElement + "' not found.");
}
// Reverse the LinkedList
LinkedList<String> reversedList = new LinkedList<>();
for (String element : linkedList) {
reversedList.addFirst(element);
}
// Print the reversed LinkedList
System.out.println("Reversed LinkedList: " + reversedList);
}
}