import java.util.Scanner; class Node { private int data; private Node next; public Node (int value, Node ref){ data = value; next = ref; } public int getData(){ return data; } public Node getNextReference(){ return next; } public void setNextReference(Node value){ next = value; } } class LinkedList { private Node head; private Node tail; public LinkedList(){ head = null; tail = null; } public void insertAtBack(int word){ if (head == null){ head = new Node(word, null); tail = head; } else{ Node tempref = tail; tail = new Node (word, null); tempref.setNextReference(tail); } } public void printList(){ if (head == null) System.out.println ("The List is Empty"); else{ Node tempref = head; System.out.println("The values so far in the list : "); while (tempref != null){ System.out.print(tempref.getData()); tempref = tempref.getNextReference(); if (tempref != null) { System.out.print (" --> "); } } System.out.println("\n"); } } } public class LinkedListTest { public static void Menu(){ System.out.println ("1 - Insert At Back "); System.out.println ("2 - Quit"); } public static void main (String args []){ System.out.println ("Linked Lists"); Scanner scanner = new Scanner (System.in); Scanner scanner1 = new Scanner (System.in); int choice = 0, status = 0, entry = 0; boolean flag = true; LinkedList list = new LinkedList(); while (flag){ Menu(); status = 0; try{ choice = Integer.parseInt(scanner.nextLine()); } catch (Exception e){ System.out.println("Integers Only - Enter Again"); status = 1; } if (choice == 1){ System.out.println ("Enter the value?"); entry = scanner1.nextInt(); list.insertAtBack(entry); } else if (choice == 2){ flag = false; System.out.println ("End of Program"); } else if (status == 0){ System.out.println ("Invalid Entry - Enter Again"); } list.printList(); } } }