From 7eb07850733333726ba94bd2c15fc4d22d728759 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Thu, 30 May 2019 12:56:17 +0530 Subject: [PATCH 1/3] Fix for Issue #773. Deleting an element that doesn't exist causes NPE --- DataStructures/Lists/DoublyLinkedList.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/DataStructures/Lists/DoublyLinkedList.java b/DataStructures/Lists/DoublyLinkedList.java index fd250d956faf..677487e1d2b0 100644 --- a/DataStructures/Lists/DoublyLinkedList.java +++ b/DataStructures/Lists/DoublyLinkedList.java @@ -116,8 +116,13 @@ public Link deleteTail() { public void delete(int x) { Link current = head; - while (current.value != x) // Find the position to delete - current = current.next; + while (current.value != x) {// Find the position to delete + if (current != tail) { + current = current.next; + } else {// If we reach the tail and the element is still not found + throw new RuntimeException("The element to be deleted does not exist!"); + } + } if (current == head) deleteHead(); From 9b7862121539191d4e831b47b57b989b7cf22460 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Thu, 30 May 2019 12:59:36 +0530 Subject: [PATCH 2/3] Revert "Fix for Issue #773. Deleting an element that doesn't exist causes NPE" This reverts commit 7eb07850733333726ba94bd2c15fc4d22d728759. --- DataStructures/Lists/DoublyLinkedList.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/DataStructures/Lists/DoublyLinkedList.java b/DataStructures/Lists/DoublyLinkedList.java index 677487e1d2b0..fd250d956faf 100644 --- a/DataStructures/Lists/DoublyLinkedList.java +++ b/DataStructures/Lists/DoublyLinkedList.java @@ -116,13 +116,8 @@ public Link deleteTail() { public void delete(int x) { Link current = head; - while (current.value != x) {// Find the position to delete - if (current != tail) { - current = current.next; - } else {// If we reach the tail and the element is still not found - throw new RuntimeException("The element to be deleted does not exist!"); - } - } + while (current.value != x) // Find the position to delete + current = current.next; if (current == head) deleteHead(); From b6581eaa168765590ec173f3b2f27a0c6558ccbb Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Thu, 30 May 2019 13:04:15 +0530 Subject: [PATCH 3/3] Fix for Issue #773. Deleting an element that doesn't exist causes NPE --- DataStructures/Lists/DoublyLinkedList.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/DataStructures/Lists/DoublyLinkedList.java b/DataStructures/Lists/DoublyLinkedList.java index fd250d956faf..677487e1d2b0 100644 --- a/DataStructures/Lists/DoublyLinkedList.java +++ b/DataStructures/Lists/DoublyLinkedList.java @@ -116,8 +116,13 @@ public Link deleteTail() { public void delete(int x) { Link current = head; - while (current.value != x) // Find the position to delete - current = current.next; + while (current.value != x) {// Find the position to delete + if (current != tail) { + current = current.next; + } else {// If we reach the tail and the element is still not found + throw new RuntimeException("The element to be deleted does not exist!"); + } + } if (current == head) deleteHead(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy