I want to insert a node after another node in a doubly-linked list but when I provide such a key that doesn't exist in the Linked list then this method gives me an error. How should I fix this error? here is my code. Thanks in Advance.
{ public void AddAfter(int insertAfter, int data) { DLNode n = new DLNode(data); DLNode curr = head; if(head == null) head = n; else { while(curr.Data != insertAfter && curr != null) { curr = curr.next; } if(curr != null) { n.next = curr.next; n.prev = curr; n.next.prev = n; curr.next = n; NumNodes++; } else System.out.println("The Key "+insertAfter+" doesn't exist: "); } } }
https://stackoverflow.com/questions/66134580/how-to-insert-a-node-after-another-node-using-doubly-linked-list February 10, 2021 at 05:54PM
没有评论:
发表评论