From dcdb9a8b0894815ed9b75f18102d675a4bf9c2c3 Mon Sep 17 00:00:00 2001 From: RanchCoder <52583285+RanchCoder@users.noreply.github.com> Date: Sun, 4 Oct 2020 18:02:43 +0530 Subject: [PATCH] Update DoublyLinkedList.java completed the remove functions on Sunday 04-01-2020 18:02 --- DoublyLinkedList.java | 148 +++++++++++++++++++++++++++++++----------- 1 file changed, 111 insertions(+), 37 deletions(-) diff --git a/DoublyLinkedList.java b/DoublyLinkedList.java index 9dde543..6ad7a29 100644 --- a/DoublyLinkedList.java +++ b/DoublyLinkedList.java @@ -76,15 +76,17 @@ public int getLength(){ public void insertAtHead(int value){ DllNode node = new DllNode(value,null,null); + //if our list is null if(length == 0){ head.setNext(node); node.setPrev(head); tail.setPrev(node); node.setNext(tail); - length += 1; + length++; return; } + //if we have more than one element in list. else{ node.setNext(head.getNext()); node.setPrev(head); @@ -97,6 +99,7 @@ public void insertAtHead(int value){ } public void insertAt(int data , int position){ + if(position < 0){ position = 0; } @@ -104,47 +107,34 @@ public void insertAt(int data , int position){ position = length; } - - DllNode node , temp = head; - - if(head == null){ - head = new DllNode(data); - tail.setPrev(head); - head.setNext(tail); - } - //when want to insert at head if(position == 0){ - insertAtHead(data); - + return; } - else if(position == length){ - - insertAtTail(data); - - } - else{ - for(int i = 1 ; i <= position ; i++){ + //when we want to insert at tail + if(position == length){ + insertAtTail(data); + return; + } + DllNode node , temp = head.getNext(); + for(int i = 1 ; i < position ; i++){ temp = temp.getNext(); } - - - + System.out.println("temp data : " + temp.getData()); //we have reached a position before now. - node = new DllNode(data,null,null); node.setNext(temp.getNext()); - node.setPrev(temp); - temp.getNext().setPrev(node); temp.setNext(node); + + length += 1; - } + @@ -165,18 +155,97 @@ public void insertAtTail(int data){ } } + // removing the first element + public int removeFirst(){ + //if no element in the list + if(length == 0){ + return Integer.MIN_VALUE; + } + DllNode temp = head.getNext(); + head.setNext(temp.getNext()); + temp.getNext().setPrev(head); + length -= 1; + return temp.getData(); + + } + //removing last element + public int removeLast(){ + //if no element in list + if(length == 0){ + return Integer.MIN_VALUE; + } + DllNode temp = tail.getPrev(); + tail.setPrev(temp.getPrev()); + temp.getPrev().setNext(tail); + length -= 1; + return temp.getData(); + + } - public int removeFirst(){return 0;} - public int removeLast(){return 0;} - public synchronized void removeMatched(DllNode node){} + + public synchronized void removeAtPosition(int position){ + if(position < 0){ + position = 0; + } + //if position greater than the no of elements starting from 0 + if(position > length - 1){ + position = length - 1; + } + // to remove first element + if(position == 0){ + removeFirst(); + return; + } + //to remove last element + if(position == (length - 1)){ + removeLast(); + return; + } + //else to remove in between element + DllNode temp = head.getNext(); + for(int i = 1 ; i < position ; i++){ + temp = temp.getNext(); + } + + + + temp.setNext(temp.getNext().getNext()); // since we reach one element before actual position , we need to set the next of current node to + //next of actual position node , so we skip the actual position node and reach its next node. + temp.getNext().setPrev(temp); // now setting prev of node after position node to point to node before position node. + length -= 1; + return; + + } + + + public synchronized void removeMatched(DllNode node){ + + if(length == 0){ + return; + } + DllNode temp = head.getNext(); + int pos = 0; + while(pos < length){ + if(node.equals(temp)){ + temp.getPrev().setNext(temp.getNext()); + temp.getNext().setPrev(temp.getPrev()); + length -= 1; + + } + pos += 1; + } + + + } public String toString(){ String result = "["; DllNode node = head.getNext(); for(int i = 0 ; i < length ; i++){ + System.out.println("Node data at " + i +" is " + node.getData()); result += node.getData(); - if( i != (length - 1)){ + if(i < length - 1){ result += ","; } node = node.getNext(); @@ -192,26 +261,31 @@ public static void main(String[] args){ DoublyLinkedList dll = new DoublyLinkedList(); dll.insertAtHead(5); System.out.println(dll.toString()); - System.out.println("list Length " + dll.getLength()); dll.insertAtHead(15); System.out.println(dll.toString()); - System.out.println("list Length " + dll.getLength()); dll.insertAtTail(25); System.out.println(dll.toString()); - dll.insertAt(35,1); + + dll.removeAtPosition(2); System.out.println(dll.toString()); - dll.insertAt(45,dll.getLength()); - System.out.println(dll.toString()); + // dll.removeFirst(); + // System.out.println(dll.toString()); + + + // dll.removeLast(); + // System.out.println(dll.toString()); + + + // dll.insertAt(35,1); + // System.out.println(dll.toString()); - dll.insertAt(65,dll.getLength() - 1); - System.out.println(dll.toString()); } }