I am trying to build LinkedList from scratch for my class. The code is here below.
I am facing this problem: when I want to insert a data, I got the message The list is empty
. Did I define the Node
variables (current, head, tail, prev, next) in a wrong way, so the node.current
is always null
?
import java.util.Scanner;
public class DoubleLL {
public static class Node {
public String name;
public Node prev;
public Node next;
}
public static boolean isListEmpty(Node head) {
if (head == null) {
return true;
} else {
return false;
}
}
public static void addFirstElement(Node head, Node tail, String Nama) {
Node temp = new Node();
temp.name = Nama;
temp.prev = null;
temp.next = null;
head = temp;
tail = temp;
}
public static void addFirst(Node head, Node tail, String Nama) { // add data from first element
if (isListEmpty(head)) {
addFirstElement(head, tail, Nama);
} else {
Node temp = new Node();
Node q = new Node();
q = head;
temp.name = Nama;
q.prev = temp;
head = temp;
temp.next = q;
temp.prev = null;
}
}
public static void addLast(Node head, Node tail, String Nama) { // add data from last element
if (isListEmpty(head)) {
addFirstElement(head, tail, Nama);
} else {
Node temp = new Node();
Node q = new Node();
q = tail;
temp.name = Nama;
q.next = temp;
tail = temp;
temp.prev = q;
temp.next = null;
}
}
public static void deleteFirst(Node head, Node tail) { //delete from first element
if (isListEmpty(head)) {
System.out.print("This list is already empty!");
System.out.print("\n");
} else if (head == tail) {
head = null;
tail = null;
} else {
Node temp = head;
head = head.next;
head.prev = null;
temp = null;
}
}
public static void deleteLast(Node head, Node tail) { // delete from last element
if (isListEmpty(head)) {
System.out.print("This list is already empty!");
System.out.print("\n");
} else if (head == tail) {
head = null;
tail = null;
} else {
Node temp = tail;
tail = tail.prev;
tail.next = null;
temp = null;
}
}
public static void retrieve(Node current) {
if (isListEmpty(current)) {
System.out.print("The list is empty.");
System.out.print("\n");
} else {
System.out.print("The linked list values are : ");
System.out.print("\n");
while (current != null) {
System.out.print(current.name);
System.out.print("\n");
current = current.next;
}
}
}
public static void main(String[] args) {
int menu;
String nameMhs;
Node head = null;
Node tail = null;
do {
System.out.print("Menu Linked List : \n");
System.out.print("1. Add First\n");
System.out.print("2. Add Last\n");
System.out.print("3. Delete First\n");
System.out.print("4. Delete Last\n");
System.out.print("5. Retrieve Linked List Values\n");
System.out.print("6. Exit\n");
System.out.print("Your choice : ");
menu = new Scanner(System.in).nextInt();
switch (menu) {
case 1:
System.out.print("Insert name : ");
namaMhs = new Scanner(System.in).nextLine();
addFirst(head, tail, nameMhs);
break;
case 2:
System.out.print("Insert name: ");
namaMhs = new Scanner(System.in).nextLine();
addLast(head, tail, nameMhs);
break;
case 3:
deleteFirst(head, tail);
break;
case 4:
deleteLast(head, tail);
break;
case 5:
retrieve(head);
break;
default:
System.out.println("Closed.");
}
System.out.print("\n");
} while (menu != 6 && menu < 6);
}
}