Skip to content

Instantly share code, notes, and snippets.

@sXakil
Last active December 17, 2018 16:40
Show Gist options
  • Select an option

  • Save sXakil/1c03a30f1e13ba7a9b0be2eb5adf96bd to your computer and use it in GitHub Desktop.

Select an option

Save sXakil/1c03a30f1e13ba7a9b0be2eb5adf96bd to your computer and use it in GitHub Desktop.
Implementation of linked list in Java
import java.util.Scanner;
class LinkedList {
static class Node {
String data;
int index;
Node next;
}
private static Node head = null;
public static void main(String[] args) {
int command = 0;
System.out.println("Enter your choice:\n" +
"1 -> Prepend\n" +
"2 -> Append\n" +
"3 -> Insert At\n" +
"4 -> Print List\n" +
"5 -> Print Item\n" +
"6 -> Delete Item");
while(command != -1) {
Scanner sc = new Scanner(System.in);
command = sc.nextInt();
switch (command) {
case 1:
prepend();
break;
case 2:
append();
break;
case 3:
insertAt();
case 4:
printList();
break;
case 5:
printItem();
break;
case 6:
deleteNode();
break;
default:
command = -1;
System.out.println("Invalid input! Program exited.");
break;
}
System.out.println("\nEnter a new choice: ");
}
}
private static void prepend() {
System.out.println("Enter the String to be prepended: ");
Scanner sc = new Scanner(System.in);
String newString = sc.nextLine();
Node newEntry = new Node();
newEntry.data = newString;
if(head == null)
newEntry.index = 0;
else
newEntry.index = head.index + 1;
newEntry.next = head;
head = newEntry;
System.out.println(newString + " was successfully added to the list.\n");
}
private static void append() {
System.out.println("Enter the String to be appended: ");
Scanner sc = new Scanner(System.in);
String newString = sc.nextLine();
Node newEntry = new Node();
newEntry.data = newString;
newEntry.index = 0;
if (head == null){
head = newEntry;
head.next = null;
}
else {
Node temp = head;
while (temp != null) {
if (temp.index == 0) break;
temp.index++;
temp = temp.next;
}
assert false;
temp.index++;
temp.next = newEntry;
newEntry.next = null;
}
System.out.println(newString + " was successfully added to the list.\n");
}
private static void insertAt() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the data: ");
String newString = sc.nextLine();
System.out.println("Enter the index where you want to insert it at: ");
int index = sc.nextInt();
Node newEntry = new Node();
newEntry.data = newString;
newEntry.index = index;
if (head != null) {
Node temp = head;
while (temp != null) {
if (temp.index == index) break;
temp.index++;
temp = temp.next;
}
assert false;
temp.index++;
newEntry.next = temp.next;
temp.next = newEntry;
}
else if(index == 0) {
head = newEntry;
head.next = null;
}
else
System.out.println("The index " + index + " doesn't exists.");
}
private static void deleteNode() {
System.out.println("Enter the index of the item you want to delete: ");
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
if (index == 0 && head != null) {
head = null;
System.out.println("The data at index " + index + " was deleted.");
}
else if (head != null) {
Node temp = head;
while (temp != null) {
if (temp.index == index) break;
temp.index--;
temp = temp.next;
}
assert false;
temp.data = temp.next.data;
temp.next = temp.next.next;
System.out.println("The data at index " + index + " was deleted.");
}
else
System.out.println("Failed to locate the data at index " + index);
}
private static void printItem()
{
System.out.println("Enter the index of the item you want to print: ");
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
int check = 0;
Node temp = head;
while (temp != null)
{
if (temp.index == index) {
System.out.print("[" + temp.data + ", " + temp.index + "]\n");
check = 1;
break;
}
temp = temp.next;
}
if (check != 1)
System.out.println("Failed to locate the data at index " + index);
}
private static void printList()
{
if (head == null)
System.out.println("List is empty.");
else {
Node temp = head;
while (temp != null) {
System.out.print("[" + temp.data + ", " + temp.index + "]\n");
temp = temp.next;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment