Add Retrieve Delete Data LinkedList Java Example
Here in this demo you will how to create you own singly linked list and how to add, retrieve and delete data from it.
Little introduction of Linked list: LinkedList is collection of linear data elements which is called nodes where each nodes pointing to next node by pointer. Because each node connected to other node it creates data structure which consists a group of nodes that makes a sequence. All we can say is each nodes will have data and reference to the next node in the sequence. Below is structure:
As you see structure above, it allows very efficient deletion and insertion of the elements from any position in sequence which doing iteration. In below java program we will create our custom linked list with three data insertion and delete one value from it but you could delete add as you needed using below program:
- JavaHonkLinkedList.java:
package com.javahonk; public class JavaHonkLinkedList { private Node nodeHead; public JavaHonkLinkedList() { nodeHead = new Node(); } public void insertData(Object x) { if (lookupData(x).equals(false)) { if (nodeHead.data == null){ nodeHead.data = x; } else { Node temp = nodeHead; while (temp.next != null) { temp = temp.next; } Node NewNode = new Node(); NewNode.data = x; NewNode.next = null; temp.next = NewNode; } } } public void deleteData(Object x) { if (lookupData(x).equals(true)) { if (nodeHead.data != null && nodeHead.data.equals(x)){ nodeHead = nodeHead.next; } else { Node temp = nodeHead; while (temp.next != null) { if ((temp.next).data !=null && (temp.next).data.equals(x)) temp.next = (temp.next).next; else temp = temp.next; } } } } public Object lookupData(Object x) { Node temp = nodeHead; Boolean search = false; if (nodeHead.data != null && nodeHead.data.equals(x)){ search = true; return search; } while (temp.next != null) { if (temp.data.equals(x)) { search = true; break; } else { temp = temp.next; } //Check for last element if (temp.data.equals(x)) search = true; } return search; } public boolean isEmpty() { if (nodeHead.next == null && nodeHead.data == null) return true; else return false; } public void printAllInsertedValues() { Node temp = nodeHead; System.out.print(temp.data + " "); while (temp.next != null) { temp = temp.next; System.out.print(temp.data + " "); } } } class Node { public Object data; public Node next; public Node() { this.data = null; this.next = null; } }
- Now test above custom linked list with below program. LlinkedListTestMain.java:
package com.javahonk; public class LlinkedListTestMain { public static void main(String[] args) { JavaHonkLinkedList linkedListTest = new JavaHonkLinkedList(); linkedListTest.insertData(new Integer(12)); linkedListTest.insertData(new Integer(99)); linkedListTest.insertData(new Integer(37)); System.out.println("List of values inserted: "); linkedListTest.printAllInsertedValues(); System.out.println("\nDelete value: 99 from Linked list"); linkedListTest.deleteData(99); System.out.println("\nAfter delete list of values available in LinkedList: "); linkedListTest.printAllInsertedValues(); } }
- Output:
Reference: