In: Computer Science
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function.
Create a menu that contains the following options :
1. Add new node at the end of LL. ( as a METHOD )
2. Add new node at the beginning of LL. ( as a METHOD )
3. Delete a node from the end of LL. ( as a METHOD )
4. Delete a node from the beginning of LL. ( as a METHOD )
5. Print all data in the LL. ( as a METHOD )
6. Exit. ( as a METHOD )
Important notes:
1. write the operations from the pseudocode.
2. The user should insert all data even in the first node (head)
3. The menu should be repeated each time until the user enter 6.
import java.io.*;
import java.util.Scanner;
public class LinkedList {
Node head, tail;
int list_size = 0;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
// Method to insert a node at the beginning of LL
public static LinkedList insertHead(LinkedList list, int data) {
Node new_node = new Node(data);
new_node.next = list.head;
list.head = new_node;
if (list.list_size == 0)
list.tail = list.head;
list.list_size++;
return list;
}
// Method to insert a node at the end of LL
public static LinkedList insertTail(LinkedList list, int data) {
Node new_node = new Node(data);
new_node.next = null;
if (list.list_size == 0) {
list.tail = new_node;
list.head = list.tail;
} else {
list.tail.next = new_node;
list.tail = new_node;
}
list.list_size++;
return list;
}
// Method to delete a node from the beginning of LL
public static LinkedList deleteHead(LinkedList list) {
if (list.list_size == 0) {
System.out.println("List is empty. Nothing to delete!");
return list;
}
if (list.list_size == 1) {
list.head = null;
list.tail = null;
list.list_size--;
return list;
}
list.head = list.head.next;
list.list_size--;
return list;
}
// Method to delete a node from the end of LL
public static LinkedList deleteTail(LinkedList list) {
if (list.list_size == 0) {
System.out.println("List is empty. Nothing to delete!");
return list;
}
if (list.list_size == 1) {
list.head = null;
list.tail = null;
list.list_size--;
return list;
}
Node iterator = list.head;
while (iterator.next.next != null) {
iterator = iterator.next;
}
list.tail = iterator;
list.tail.next = null;
list.list_size--;
return list;
}
// Method to print the LL.
public static void printList(LinkedList list) {
Node currNode = list.head;
System.out.print("LinkedList: ");
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
while (true) {
int choice;
System.out.println("Choose a number from below menu:");
System.out.println("1. Add new node at the end of LL.");
System.out.println("2. Add new node at the beginning of LL.");
System.out.println("3. Delete a node from the end of LL.");
System.out.println("4. Delete a node from the beginning of LL.");
System.out.println("5. Print all data in the LL.");
System.out.println("6. Exit.");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
if (choice == 6)
break;
if (choice == 1) {
System.out.println("Enter the number to be added");
int data = sc.nextInt();
list = insertTail(list, data);
} else if (choice == 2) {
System.out.println("Enter the number to be added");
int data = sc.nextInt();
list = insertHead(list, data);
} else if (choice == 3)
list = deleteTail(list);
else if (choice == 4)
list = deleteHead(list);
else if (choice == 5)
printList(list);
}
}
}