import java.util.Scanner;
class Node {
int data;
Node next;
}
public class linkedlist {
static Node head;
public static void main(String[] args) {
int choice = 0;
Scanner scanner = new Scanner(System.in);
while (choice != 9) {
System.out.println("\n\n*********Main Menu*********");
System.out.println("\nChoose one option from the following list ...");
System.out.println("\n===============================================");
System.out.println("\n1.Insert in beginning\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\n9.Exit\n");
System.out.println("\nEnter your choice?");
choice = scanner.nextInt();
switch (choice) {
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
System.exit(0);
break;
default:
System.out.println("Please enter valid choice..");
}
}
}
static void beginsert() {
Node ptr;
int item;
ptr = new Node();
Scanner scanner = new Scanner(System.in);
if (ptr == null) {
System.out.println("\nOVERFLOW");
} else {
System.out.println("\nEnter value");
item = scanner.nextInt();
ptr.data = item;
ptr.next = head;
head = ptr;
System.out.println("\nNode inserted");
}
}
static void lastinsert() {
Node ptr, temp;
int item;
ptr = new Node();
Scanner scanner = new Scanner(System.in);
if (ptr == null) {
System.out.println("\nOVERFLOW");
} else {
System.out.println("\nEnter value?");
item = scanner.nextInt();
ptr.data = item;
if (head == null) {
ptr.next = null;
head = ptr;
System.out.println("\nNode inserted");
} else {
temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = ptr;
ptr.next = null;
System.out.println("\nNode inserted");
}
}
}
static void randominsert() {
int i, loc, item;
Node ptr, temp;
ptr = new Node();
Scanner scanner = new Scanner(System.in);
if (ptr == null) {
System.out.println("\nOVERFLOW");
} else {
System.out.println("\nEnter element value");
item = scanner.nextInt();
ptr.data = item;
System.out.println("\nEnter the location after which you want to insert ");
loc = scanner.nextInt();
temp = head;
for (i = 0; i < loc; i++) {
temp = temp.next;
if (temp == null) {
System.out.println("\ncan't insert\n");
return;
}
}
ptr.next = temp.next;
temp.next = ptr;
System.out.println("\nNode inserted");
}
}
static void begin_delete() {
Node ptr;
if (head == null) {
System.out.println("\nList is empty");
} else {
ptr = head;
head = ptr.next;
ptr = null;
System.out.println("\nNode deleted from the beginning ...");
}
}
static void last_delete() {
Node ptr, ptr1;
if (head == null) {
System.out.println("\nlist is empty");
} else if (head.next == null) {
head = null;
System.out.println("\nOnly node of the list deleted ...");
} else {
ptr = head;
while (ptr.next != null) {
ptr1 = ptr;
ptr = ptr.next;
}
ptr.next = null;
ptr = null;
System.out.println("\nDeleted Node from the last ...");
}
}
static void random_delete() {
Node ptr, ptr1;
int loc, i;
Scanner scanner = new Scanner(System.in);
System.out.println("\n Enter the location of the node after which you want to perform deletion \n");
loc = scanner.nextInt();
ptr = head;
for (i = 0; i < loc; i++) {
ptr1 = ptr;
ptr = ptr.next;
if (ptr == null) {
System.out.println("\nCan't delete");
return;
}
}
ptr.next = ptr.next;
ptr = null;
System.out.println("\nDeleted node " + (loc + 1));
}
static void search() {
Node ptr;
int item, i = 0, flag = 0;
Scanner scanner = new Scanner(System.in);
ptr = head;
if (ptr == null) {
System.out.println("\nEmpty List");
} else {
System.out.println("\nEnter item which you want to search?");
item = scanner.nextInt();
while (ptr != null) {
if (ptr.data == item) {
System.out.println("item found at location " + (i + 1));
flag = 0;
} else {
flag = 1;
}
i++;
ptr = ptr.next;
}
if (flag == 1) {
System.out.println("Item not found");
}
}
}
static void display() {
Node ptr;
ptr = head;
if (ptr == null) {
System.out.println("Nothing to print");
} else {
System.out.println("\nprinting values . . . . .");
while (ptr != null) {
System.out.println("\n" + ptr.data);
ptr = ptr.next;
}
}
}
}
Top comments (0)