Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Created September 21, 2019 19:01
Show Gist options
  • Select an option

  • Save julianjupiter/43f97968336d2b309207a18ef7e5fecf to your computer and use it in GitHub Desktop.

Select an option

Save julianjupiter/43f97968336d2b309207a18ef7e5fecf to your computer and use it in GitHub Desktop.
LinkedList - Add, Remove, Modify and Display Nodes
import java.io.IOException;
import java.util.Scanner;
public class LinkedList {
private final Scanner input = new Scanner(System.in);
private Node head;
private class Node {
String data;
int intKey;
Node next;
public Node(String data, int intKey) {
this.data = data;
this.intKey = intKey;
this.next = null;
}
@Override
public String toString() {
return "Node{" + "data=" + data + ", intKey=" + intKey + ", next=" + next + '}';
}
}
public void addNodeAtLast(Node newNode) {
Node first = this.head;
if (first == null) {
this.head = newNode;
} else {
while (first.next != null) {
first = first.next;
}
first.next = newNode;
}
System.out.println("Successful!");
this.printNodes();
}
public void addNodeAtFirst(Node newNode) {
Node first = this.head;
if (first == null) {
this.head = newNode;
} else {
newNode.next = first;
this.head = newNode;
}
System.out.println("Successful!");
this.printNodes();
}
public void addNodeAtKey(int intKey, Node newNode) {
Node first = this.head;
boolean flag = false;
if (first != null) {
if (first.intKey == intKey) {
newNode.next = first;
this.head = newNode;
flag = true;
} else {
while (first.next != null) {
if (first.next.intKey == intKey) {
newNode.next = first.next;
first.next = newNode;
flag = true;
break;
}
first = first.next;
}
}
System.out.println("Successful!");
}
if (!flag) {
System.out.println("No Node with key " + intKey);
}
this.printNodes();
}
public void removeNodeAtFirst() {
Node first = this.head;
if (first != null) {
this.head = first.next;
System.out.println("Successful!");
} else {
System.out.println("No Node to remove.");
}
this.printNodes();
}
public void removeNodeAtLast() {
Node first = this.head;
if (first != null) {
Node previous = null;
while (first != null && first.next != null) {
previous = first;
first = first.next;
}
if (previous != null) {
previous.next = null;
} else {
this.head = null;
}
System.out.println("Successful!");
} else {
System.out.println("No Node to remove.");
}
this.printNodes();
}
public void removeNodeAtKey(int intKey) {
Node first = this.head;
boolean flag = false;
if (first != null) {
if (first.intKey == intKey) {
if (first.next != null) {
this.head = first.next;
} else {
this.head = null;
}
flag = true;
} else {
while (first.next != null) {
if (first.next.intKey == intKey) {
if (first.next.next != null) {
first.next = first.next.next;
} else {
first.next = null;
}
flag = true;
break;
}
first = first.next;
}
}
System.out.println("Successful!");
}
if (!flag) {
System.out.println("No Node with key " + intKey);
}
this.printNodes();
}
public void modifyNode(Node node) {
Node first = this.head;
boolean flag = false;
if (first != null) {
if (first.intKey == node.intKey) {
first.data = node.data;
flag = true;
} else {
while (first.next != null) {
if (first.next.intKey == node.intKey) {
first.next.data = node.data;
flag = true;
break;
}
first = first.next;
}
}
System.out.println("Successful!");
}
if (!flag) {
System.out.println("No Node with key " + node.intKey);
}
this.printNodes();
}
public void printNodes() {
System.out.println("List of Node(s):");
Node node = this.head;
if (node != null) {
while (node.next != null) {
System.out.println(node);
node = node.next;
}
System.out.println(node);
} else {
System.out.println("No Node exists.");
}
}
public static void main(String args[]) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of initial Node(s): ");
int numberOfNode = input.nextInt();
LinkedList linkedList = new LinkedList();
linkedList.createInitialNodes(numberOfNode);
linkedList.operation();
System.out.println("Bye!");
}
private void createInitialNodes(int numberOfNode) throws IOException {
int counter = 1;
while (numberOfNode > 0) {
System.out.println("Node(" + counter++ + ")");
Node node = createNode();
this.addNodeAtLast(node);
numberOfNode--;
System.out.println("-----");
}
}
private void operation() {
this.menu();
System.out.print("\nSelect operation: ");
int operation = input.nextInt();
System.out.println();
while (operation != 0) {
Node node;
int intKey;
switch (operation) {
case 1:
System.out.println("Add Node at the beginning");
node = this.createNode();
this.addNodeAtFirst(node);
break;
case 2:
System.out.println("Add Node at the end/tail");
node = this.createNode();
this.addNodeAtLast(node);
break;
case 3:
System.out.println("Add Node at key value");
System.out.print("Enter key value: ");
intKey = input.nextInt();
node = this.createNode();
this.addNodeAtKey(intKey, node);
break;
case 4:
System.out.println("Remove Node at the beginning");
this.removeNodeAtFirst();
break;
case 5:
System.out.println("Remove Node at the end/tail");
this.removeNodeAtLast();
break;
case 6:
System.out.println("Remove Node by key value");
System.out.print("Enter key value: ");
intKey = input.nextInt();
this.removeNodeAtKey(intKey);
break;
case 7:
System.out.println("Modify data of a Node");
node = createNode();
this.modifyNode(node);
break;
case 8:
System.out.println("Display Nodes(s)");
this.printNodes();
break;
default:
System.out.println("Please try again!");
}
this.menu();
System.out.print("\nSelect operation: ");
operation = input.nextInt();
System.out.println();
}
}
private void menu() {
System.out.println("\nMENU");
System.out.println("[1] Add Node at the beginning");
System.out.println("[2] Add Node at the end/tail");
System.out.println("[3] Add Node at key value");
System.out.println("[4] Remove Node at the beginning");
System.out.println("[5] Remove Node at the end/tail");
System.out.println("[6] Remove Node by key value");
System.out.println("[7] Modify data of a Node");
System.out.println("[8] Display Nodes(s)");
System.out.println("[0] Exit");
}
private Node createNode() {
System.out.print("Enter data: ");
String data = input.next();
System.out.print("Enter key: ");
int intKey = input.nextInt();
return new Node(data, intKey);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment