Created
March 23, 2023 10:24
-
-
Save Rohit-554/3962f0f8bd176c155ced652a2cd5de3c to your computer and use it in GitHub Desktop.
LinkedListImplementationJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LL { | |
| Node head; | |
| private int size; | |
| LL () { | |
| size = 0; | |
| } | |
| public class Node { | |
| String data; | |
| Node next; | |
| Node(String data) { | |
| this.data = data; | |
| this.next = null; | |
| size++; | |
| } | |
| } | |
| public void addFirst(String data) { | |
| Node newNode = new Node(data); | |
| if(head == null){ | |
| head = newNode; | |
| return; | |
| } | |
| newNode.next = head; | |
| head = newNode; | |
| } | |
| public void addLast(String data) { | |
| Node newNode = new Node(data); | |
| if(head == null) { | |
| head = newNode; | |
| return; | |
| } | |
| Node currNode = head; | |
| while(currNode.next != null) { | |
| currNode = currNode.next; | |
| } | |
| currNode.next = newNode; | |
| } | |
| public void printList() { | |
| if(head == null){ | |
| System.out.println("List is empty"); | |
| return; | |
| } | |
| Node currNode = head; | |
| while(currNode != null) { | |
| System.out.print(currNode.data+" -> "); | |
| currNode = currNode.next; | |
| } | |
| System.out.println("null"); | |
| } | |
| public void removeFirst() { | |
| if(head == null) { | |
| System.out.println("Empty List, nothing to delete"); | |
| return; | |
| } | |
| head = this.head.next; | |
| size--; | |
| } | |
| public void removeLast() { | |
| if(head == null) { | |
| System.out.println("Empty List, nothing to delete"); | |
| return; | |
| } | |
| size--; | |
| if(head.next == null) { | |
| head = null; | |
| return; | |
| } | |
| //second last | |
| Node secondLast = head; | |
| Node lastNode = head.next; | |
| while(lastNode.next != null) { | |
| secondLast = secondLast.next; | |
| lastNode = lastNode.next; | |
| } | |
| secondLast.next = null; | |
| } | |
| public int getSize() { | |
| return size; | |
| } | |
| public void addInMiddle(int index, String data) { | |
| if(index > size || index < 0) { | |
| System.out.println("Invalid Index value"); | |
| return; | |
| } | |
| size++; | |
| Node newNode = new Node(data); //creation of new node | |
| if(head == null || index == 0) { | |
| newNode.next = head; | |
| head = newNode; | |
| return; | |
| } | |
| Node currNode = head; | |
| for(int i=1; i<size; i++) { | |
| if(i == index) { | |
| Node nextNode = currNode.next; | |
| currNode.next = newNode; | |
| newNode.next = nextNode; | |
| break; | |
| } | |
| currNode = currNode.next; | |
| } | |
| } | |
| public static void main(String args[]) { | |
| LL list = new LL(); | |
| list.addLast("is"); | |
| list.addLast("a"); | |
| list.addLast("list"); | |
| list.addFirst("this"); | |
| list.printList(); | |
| list.addInMiddle(2, "William"); | |
| list.printList(); | |
| // list.addFirst("this"); | |
| // list.printList(); | |
| // System.out.println(list.getSize()); | |
| // list.removeFirst(); | |
| // list.printList(); | |
| // list.removeLast(); | |
| // list.printList(); | |
| // System.out.println(list.getSize()); | |
| // list.addInMiddle(2, "rohit"); | |
| // list.printList(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment