basics using linked list
Operation Using Linked List in Data Structure: Traversal, Insertion, Deletion and Searching
import java.util.Scanner;
// Node class represents each element in the linked list
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
// Custom LinkedList class to manage linked list operations
class LinkedList {
private Node head;
// Function to insert an element at the end of the linked list
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
// Function to insert an element at a specific index in the linked list
public void insertAtIndex(int data, int index) {
Node newNode = new Node(data);
if (index < 0) {
System.out.println("Invalid index. Insertion failed.");
return;
}
if (index == 0) {
// Insert at the beginning
newNode.next = head;
head = newNode;
} else {
// Insert at a specific index
Node current = head;
int currentIndex = 0;
while (current != null && currentIndex < index - 1) {
current = current.next;
currentIndex++;
}
if (current == null) {
System.out.println("Invalid index. Insertion failed.");
return;
}
newNode.next = current.next;
current.next = newNode;
}
}
// Function to traverse and print the linked list
public void traverse() {
Node current = head;
System.out.println("Traversal:");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Function to delete an element from the linked list
public void delete(int data) {
if (head == null) {
System.out.println("The list is empty. Deletion failed.");
return;
}
if (head.data == data) {
head = head.next;
System.out.println("Element " + data + " deleted.");
return;
}
Node current = head;
Node prev = null;
while (current != null && current.data != data) {
prev = current;
current = current.next;
}
if (current == null) {
System.out.println("Element " + data + " not found. Deletion failed.");
} else {
prev.next = current.next;
System.out.println("Element " + data + " deleted.");
}
}
// Function to search for an element in the linked list
public boolean search(int data) {
Node current = head;
while (current != null) {
if (current.data == data) {
return true;
}
current = current.next;
}
return false;
}
}
// Main class to interactively perform linked list operations
class LinkedListOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList linkedList = new LinkedList();
while (true) {
System.out.println("Select an operation:");
System.out.println("1. Traversal");
System.out.println("2. Insertion at the end");
System.out.println("3. Insertion at a specific index");
System.out.println("4. Deletion");
System.out.println("5. Searching");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
linkedList.traverse();
break;
case 2:
System.out.print("Enter the value to insert at the end: ");
int valueToInsert = scanner.nextInt();
linkedList.insert(valueToInsert);
break;
case 3:
System.out.print("Enter the value to insert: ");
int value = scanner.nextInt();
System.out.print("Enter the index to insert at: ");
int index = scanner.nextInt();
linkedList.insertAtIndex(value, index);
break;
case 4:
System.out.print("Enter the element to delete: ");
int elementToDelete = scanner.nextInt();
linkedList.delete(elementToDelete);
break;
case 5:
System.out.print("Enter the element to search for: ");
int target = scanner.nextInt();
boolean found = linkedList.search(target);
if (found) {
System.out.println("Element " + target + " found.");
} else {
System.out.println("Element " + target + " not found.");
}
break;
case 6:
System.out.println("Exiting program.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}
Comments
Post a Comment