I'm trying to better undserstand my teachers notes on how to delete a node from a doubly linked list, what she had on the boards is
public void deleteNode(Node D){
Node current = head;
while(current.data != D.data && current.next != null){
current = current.next;
}
d.prev.next = d.next;
d.next.prev = current.prev.
}
I can't help but feel like this isn't enough to remove a node. I was thinking maybe she meant
current.prev.next = d.next and
current.next.prev = d.prev
Once I figure out how to understand this better would it make sense if I wanted to delete a node from the middle by doing
public void deleteMiddle(){
Node current = head;
int i = 0;
while(i < size/2){
current = current.next;
i++;
}
deleteNode(current);
}
The correct way would be to either pass in the value, i.e. the data, or to make the method private, to protect against misuse.
Or do both:
public void deleteNode(int data) {
Node current = head;
while (current != null && current.data != data) {
current = current.next;
}
deleteNode(current);
// Note: We silently do nothing if 'data' not found
}
private void deleteNode(Node node) {
if (node != null) {
// Here we can rely on 'node' actually being in our list
if (node.prev != null)
node.prev.next = node.next;
else
head = node.next;
if (node.next != null)
node.next.prev = node.prev;
else
tail = node.prev;
}
}
Related
I have been trying to write a method to take in a linked list and sort it using a priority queue
I have no clue how to write this code I understand how a priority queue is supposed to work but I don't know how to put that into code
Override
public void priorityEnqueue(AnyType x) {
if (isEmpty()){
back = front = new ListNode<>(x);
}
{
back=back.next = new ListNode<>(x);
}
}
I am aware of the fact that this only returns the list as is without sorting it and that is exactly what I need help with
This is the answer I figured it out
#Override
public void priorityEnqueue(AnyType x) {
ListNode<AnyType> node = new ListNode<>(x);
if(front == null){
front = node;
back = node;
}
else{
if(front.element.compareTo(x) > 0){
node.next = front;
front = node;
}
else{
ListNode<AnyType> current = front;
while(current.next != null && current.next.element.compareTo(x) < 0)
current = current.next;
node.next = current.next;
current.next = node;
if(node.next == null)
back = node;
}
}
}
I have an implementation of doubly linked list , and I'm trying to delete a particular node at a given position. I managed to delete the second node to the last node but when I try to delete the first node it fails, I wonder what's wrong with my code.
I've already tried this but still doesn't work
head.next.previous = null;
head = head.next;
This is my code
public class Proses {
private class Node{
String Matkul;
int NilaiUts;
int NilaiUAS;
Node previous;
Node next;
public Node(String Matkul, int Nilai, int NilaiUAS) {
this.Matkul = Matkul;
this.NilaiUts = Nilai;
this.NilaiUAS = NilaiUAS;
}
}
Node head, tail = null;
public void addNode(String matkul, int Nilai, int NilaiUAS) {
Node newNode = new Node(matkul, Nilai, NilaiUAS);
if(head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
} else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
public void delete(int position){
if (head == null || n <= 0)
return;
Node current = head;
int i;
for (i = 1; current != null && i < position; i++)
{
current = current.next;
}
if (current == null)
return;
deleteNode(head, current);
}
//delete function
public Node deleteNode(Node head, Node del){
if (head == null || del == null){
return null;
}
if (head == del){
head = del.next;
del.next.previous = null;
}
if (del.next != null){
del.next.previous = del.previous;
}
if (del.previous != null){
del.previous.next = del.next;
}
del = null;
return head;
}
}
Using your code, if the scenario is such that it ends up with 1 node (head will be pointing to this node) and you want to delete this node (i.e. head), code will fail with NullPointerException at
del.next.previous = null;
as del.next is NULL;
Use can take a look at below code to delete a Node from doubly linked list
// Function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> data of node to be deleted.
void deleteNode(Node head_ref, Node del)
{
// Base case
if (head == null || del == null) {
return;
}
// If node to be deleted is head node
if (head == del) {
head = del.next;
}
// Change next only if node to be deleted
// is NOT the last node
if (del.next != null) {
del.next.prev = del.prev;
}
// Change prev only if node to be deleted
// is NOT the first node
if (del.prev != null) {
del.prev.next = del.next;
}
// Finally, free the memory occupied by del
return;
}
code ref: https://www.geeksforgeeks.org/delete-a-node-in-a-doubly-linked-list/
The problem with your code is that, head is not getting changed in deleteNode function because it's pass by value. Consider the following scenario:
You are deleting the position 1. Head is pointing to the node1, so
it store the address of node1. assume it's 1001.
Now you call deleteNode function with head reference and currentNode, so head reference is get passed to the function argument as pass by value. so in function argument head contains the address 1001.
Now you perform the delete operation, so the function's head is changing it's position to the next node. But, your class member's head is still pointing to the first position.
To overcome this, you can set the head again, because you are returning it from the deleteNode function. Like:
Change the code as follow
public void delete(int position){
if (head == null || n <= 0)
return;
Node current = head;
int i;
for (i = 1; current != null && i < position; i++)
{
current = current.next;
}
if (current == null)
return;
head = deleteNode(head, current);
}
would anybody be able to help me with this problem I've been having? I'm trying to implement a doubly linked list for the first time and have been frustrated trying to get my code to work for my remove class:
public void remove(int data) {
if(length == 0) {
return;
}
if (head.data == data){
head = head.next;
length--;
}
Node current = head;
while (current != null) {
if(current.data != data) {
current = current.next;
length--;
}
current = current.next;
}
}
I know this question may have been asked before, but I was unable to understand the differences in other people's code as it was much more complex. It's been a really difficult day trying to get this one method working without getting a NullPointerException and help would be appreciated! Thanks.
You want to remove a single node or part of the front list?
If you are trying to remove a single node, your current node is used as a pointer to find the node to be removed, when you find such node, you can change the node's value, if this is the last element, you can set the previous node's next node to null, else set the previous nodes' next node to the node's next node, and change the node's next node's previous node to the node's previous node.
public void remove(int data) {
if(length == 0) {
return;
}
if (head.data == data){
head = head.next;
length--;
}
Node current = head;
while (current != null) {
if (current.data != data) {
current = current.next;
} else {
if (current.next == null) {
current.pre.next = null;
} else {
current.pre.next = current.next;
current.next.pre = current.pre;
}
length--;
}
}
}
Try this:
public void remove(int data) {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (this.size == 1 && this.head.getData() == data) {
this.head = this.tail = null;
this.size--;
return;
}
if (this.head.getData() == data) {
this.head = this.head.getNext();
this.head.setPrevious(null);
this.size--;
return;
}
if (this.tail.getData() == data) {
this.tail = this.tail.getPrevious();
this.tail.setNext(null);
this.size--;
return;
}
for (Node<T> current = this.head; current != null; current = current.getNext()) {
if (current.getData() == data) {
Node<T> previous = current.getPrevious();
Node<T> next = current.getNext();
previous.setNext(next);
next.setPrevious(previous);
this.size--;
return;
}
}
throw new NoSuchElementException();
}
the method 'insertAscending' only gives me the first number even after i enter new ones. can anyone help with what i'm doing wrong? Thanks.
public class LinkedList13 {
// Private inner class Node
private class Node{
int data;
Node link;
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList13(){
head = null;
}
public void insertAscending(int data){
Node node = new Node();
node.data = data;
if (head == null)
head = node;
Node p = head;
while (p.link != null)
{
if (p.link.data > data)
{ node.link = p.link;
p.link = node;
break;
}
p= p.link;
}
}
}
Hint: is (p.link != null) ever true?
First of all, you should return after setting the head of the list (when the first element is added).
Second of all, you should handle the case where the newly inserted node is the smallest in the list (and therefore should come first). Your loop never compares the added node to the head of the list.
Finally, if the added element wasn't inserted in the while loop, it should be inserted after the while loop.
public void insertAscending(int data)
{
Node node = new Node();
node.data = data;
if (head == null) {
head = node;
return;
} else if (node.data < head.data) {
node.link = head;
head = node;
return;
}
Node p = head;
boolean added=false;
while (p.link != null)
{
if (p.link.data > data)
{
node.link = p.link;
p.link = node;
added = true;
break;
}
p = p.link;
}
if (!added)
p.link = node;
}
Check out your if condition if(p.link.data > data) the only way a node gets into the list is when that is true. This means, that, if the value of data being inserted it greater than (or equal to) everything that's been inserted so far, it will be discarded.
An easy way to fix this is change break to return and add p.link=node at the end (after the loop).
I'm working on a method that is supposed to delete the node prior to the last one,the logic seems quite fine with me, but when I tried to implement it in a project, it didn't work out. ( Oh and I'm using MyLinkedList)
here's the code:
public void deleteSec(){
Node current = head;
Node p = head.next;
Node q = null;
while(p.next!=null){
q = current;
current.next = p;
p = p.next;
}
q.next = p; // q.next = q.next.next;
}
What if your LL is empty? head will be null and this will cause an exception when you call head.next;
you have to take care of special cases like: empty LL, LL with one node, LL with two nodes.
Here is my code:
public void deleteSec() {
if (head == null) {
return;
}
if (head.next == null) {
return;
}
if (head.next.next == null) {
head = head.next;
return;
}
Node current = head;
Node p = current.next;
Node q = p.next;
while (q.next != null) {
current = current.next;
p = p.next;
q = q.next;
}
current.next = q;
}
if(myLinkedList.size() > 1) {
myLinkedList.remove(myLinkedList.size()-2);
}
well i personally compiled it,
Assuming the node class is named Node and you have a getNext() method that returns the next Node or null if this Node is the last node, you would do something like this.
if (head == null) // or if (first == null)
{
return; // There are no elements in the list.
}
Node currect = head; // This is the head, or Node current = first;
Node previous = null;
while (current.getNext() != null)
{
previous = current;
currrent = current.getNext();
}
Then do this to make the second to last pointer to next null.
if (previous != null)
{
previous.setNext( null );
}
else
{
// The list has 1 entry only.
head = null; // or first = null;
}
If deleting a second last node would be a common operation, as it is in my case, I would suggest an extra prev or previous node added to the Node construction.
Usually a linked list node would be
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
But I modified it to be like
private static class Node<Item> {
private Item item;
private Node<Item> prev;
private Node<Item> next;
}
Thus, if you want to delete the second last, the implementation would be pretty straightforward:
oldSecondLast = last.prev; // Assumes last points to the last node
oldSecondLast.next = last;
last = oldSecondLast.prev;
oldSecondLast = null; // To avoid loitering