Java Linked List "insertAfter" - method - java

public void insertAfter(String after, String newName, int newPunkte)
{
ListNode newNode = new ListNode(newName, newPunkte, null);
if(head == null)
{
System.out.println("'InsertAfter' is not possible.");
return;
}
else
{
current = head;
while(current != null)
{
if(current.getName().equals(after))
{
System.out.println(after+" was found. "+newName+" was created.");
//Here is my problem...
current.setNext(newNode);
return;
}
previous = current;
current = current.getNext();
}
System.out.println(after+" was not found.");
return;
}
}
Hey guys, Ive got a little problem with my code. When I insert a new Node after
a (if so) found Node, the following Nodes (after the new one) are disappearing..
I pretty sure that the problem is that i didnt set "previous" after inserting.
Im pretty inexperienced with implementing Linked Lists.
I hope you can help me:)
By the way for understanding my code: The parameter "newPunkte" means "newPoints".

You need to set the next node on newNode:
ListNode next = current.getNext();
current.setNext(newNode);
newNode.setNext(next);

Related

I cannot get my doubly linked list to delete a node within the list properly, it looks like it should work but doesn't. What am I doing wrong?

So, I have created a class that generates a generic doubly linked list. The problem is that the tester file keeps saying that I have done something incorrectly. It looks like it starts having problems with the deleting nodes portion of the test.
This is the deletion portion of the tester.
(The original numbers are 0,1,2,3,4,5,6,7,8,9 and the end result should be 1,3,4,5,6,7,8)
public static boolean deleteTest(GenDoubleLinkedList<Integer> intList)
{
printDecorated("Removing Test\nRemoving first item, third item, and last item");
intList.resetCurrent();
intList.deleteCurrent();
intList.goToNext();
intList.deleteCurrent();
while(intList.moreToIterate())
{
intList.goToNext();
}
intList.deleteCurrent();
intList.print();
return valuesMatch(intList,TEST_VALS_2);
}
These are my deleteCurrent, moreToIterate, resetCurrent and goToNext methods.
public void deleteCurrent() {
if(current != null && prev != null) {
System.out.println("DELETING " + current.data);
current = current.nextLink;
prev.nextLink = current;
next.prevLink = current;
}
else if(current != null && prev == null) {
System.out.println("DELETING " + current.data);
head = head.nextLink;
current = current.nextLink;
}
}
public boolean moreToIterate() {
//System.out.println(current.nextLink != null);
if(current.nextLink != null)
return true;
return false;
}
public void resetCurrent() {
current = head;
prev = null;
next = current.nextLink;
}
public void goToNext() {
//System.out.println(current.data);
if(current != null) {
current = current.nextLink;
prev = current.prevLink;
next = current.nextLink;
}
}
(I have the System.out.println()'s to troubleshoot.)
Here is what the console is outputting.
From my system out it looks like it should be deleting the correct nodes but when the test runs it obviously did not delete the correct ones. I am at a standstill and cannot figure out the issue.
If I need to I can give the entire tester file along with any of my code. Any help is appreciated.
current = current.nextLink should be changed to current = prev in your delete routine and current = current.nextLink should be removed from your "delete head" portion of your delete routine.
Please next time paste your cade it code tags so we can copy paste it to refer to it and also the line numbers...

Removing Duplicates from sorted LinkedList Relation b/w "slow" and "head"

Can someone please explain how it is getting the correct output when we are not updating the "head"?
public ListNode deleteDuplicates(ListNode head) {
ListNode slow = head;
while (slow.next != null) {
if (slow.val == slow.next.val) {
slow.next = slow.next.next;
}
else {
slow = slow.next;
}
}
return head;
Here the ListNode object reference is passed as a value to deleteDuplicates and the same reference is assigned from head to slow (slow = head;), so after this method is returned your ListNode object will hold the change
for more clarification please check this
Thank you.

could this remove method for Linkedlist work?

I asked my friends about this and they said you can never take the current to the previous node and when I asked why they didn't give me a clear reason can anyone help me?
//here is the signature of the method;
public void remove (){
Node<T> tmp = null;
tmp.next = head;
// I want to delete the current by the way!;
while (tmp.next != current)
tmp = tmp.next;
tmp.next = current.next;
//now am taking the current to the node before it so that it only becomes null if the linkedlist is empty;
current=tmp;
}
The basic idea is sound if you are looking to mutate an existing linked list and remove an element. Although there is a lack of curly braces after the while and a NullPointException that will occur at the first tmp.next.
Generally a remove method would be passed the data in the node to remove. It's unclear in your question what current is supposed to point to. Your code does not support removing the head of the list or empty lists (null head).
Here's a potential implementation:
public boolean remove(T value) {
if (head == null) {
return false;
} else if (head.value.equals(value)) {
head = head.next;
return true;
} else {
Node<T> current = head;
while (current.next != null) {
if (current.next.value.equals(value)) {
current.next = current.next.next;
return true;
}
current = current.next;
}
return false;
}
}
If the current variable is supposed to end up pointing to the node before the removed node then you have a problem: what happens if you are removing the head of the list?

Java Linked List search and delete method

I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need to be able to delete all nodes (which is done) and delete a single specified node. So I need to search through the list of nodes find the one to delete and delete it. Anything that can help is appreciated. If you have a solution please offer an explanation as I am trying to learn and just solve the problem.
I'm not going to give you the GUI because I don't think it is necessary but here is the node class.
public class MagazineList {
private MagazineNode list;
public MagazineList(){
list = null;
}
public void add(Magazine mag){
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if(list == null) {
list = node;
}
else {
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public void insert(Magazine mag) {
MagazineNode node = new MagazineNode (mag);
// make the new first node point to the current root
node.next=list;
// update the root to the new first node
list=node;
}
public void deleteAll() {
if(list == null) {
}
else {
list = null;
}
}
public void delete(Magazine mag) {
//Delete Method Goes Here
}
public String toString(){
String result = " ";
MagazineNode current = list;
while (current != null){
result += current.magazine + "\n";
current = current.next;
}
return result;
}
private class MagazineNode {
public Magazine magazine;
public MagazineNode next;
public MagazineNode(Magazine mag){
magazine = mag;
next = null;
}
}
}
UPDATE
Here is the method I put together and it goes through the first part into the while loop and never recognizes the same item in the list. I used the exact same thing for the input and delete methods yet it will not recognize it. Any help is appreciated.
public void delete (Magazine mag) {
MagazineNode current = list;
MagazineNode before;
before = current;
if(current.equals(mag)){
before.next = current;
System.out.println("Hello");
}
while ((current = current.next)!= null){
before = current.next;
System.out.println("Hello Red");
if(current.equals(mag)){
current = before;
System.out.println("Hello Blue");
}
}
}
Without spoon feeding you the answer. deletion is a bit like removing one link of a chain - you cut out the link and join up the two (new) ends.
So, deleting "B" would mean changing
A --> B --> C --> D
to this
A --> C --> D
In pseudo code, the algorithm would be:
start the algorithm with the first node
check if it is the one you want to delete
if not, go to the next node and check again (go back to the previous step)
if so, make the next node of the previous node the next node of this node
remove the reference from this node to the next node
public void delete (Magazine mag) {
MagazineNode current = this.list;
MagazineNode before;
//if is the first element
if (current.equals(mag)) {
this.list = current.next;
return; //ending the method
}
before = current;
//while there are elements in the list
while ((current = current.next) != null) {
//if is the current element
if (current.equals(mag)) {
before.next = current.next;
return; //endind the method
}
before = current;
}
//it isnt in the list
}
the comments should explains whats happening
All you need to do is search through the list, keeping track of where you are. When the node you want to delete is in front of you, set the current node's "next" to the one after the one you want to delete:
for(Node current = list; current.next() != null; current = current.next()){
if(current.next().magazine().equals(toDelete)) current.setNext(current.next().next());
}
Something like that.
Hope that helps!

Removing a node from a doubly linked list?

Here's the linked list code
public class LList
{
protected int size;
protected DNode tail, header;
public LList()
{
size = 0;
tail = null;
header = tail;
}
public void addDNode(DNode v)
{
// means list is empty, so add first element
if (header == null)
{
header = v;
tail = header; // first element so (head == tail)
}
else
{
tail.setNext(v);
v.setPrev(tail);
v.setNext(null);
tail = v;
}
size++;
}
and the remove node method that's the problem
public DNode removeDnode(DNode current)
{
if(current.nextNode() == null)
{
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
}
else if (current.prevNode() == null)
{
DNode next = current.nextNode();
next.setPrev(null);
current.setNext(null);
}
else
{
DNode next = current.nextNode();
DNode previous = current.prevNode();
previous.setNext(next);
next.setPrev(previous);
current.setPrev(null);
current.setNext(null);
}
size = size - 1;
return current;
}
The problem is that when I use previous.setNext(null); it won't let me add a node again which I think it has something to do with the header and tail.
however when i use previous.setNext(tail); it doesn't seem to remove it from the list??
Aren't you forgetting to point the tail of the list to the previous element when you remove the last?
if(current.nextNode() == null) {
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
tail = previous; //isn't this missing?!
}
Problem is, if you remove tail, you must move it. So try
DNode previous = current.prevNode();
previous.setNext(null);
current.setPrev(null);
tail = previous;
You have the same problem with the next—but I believe once you're done with tail, you'll fix it easily.
Handling remove from a doubly-linked list is VERY tricky. When you see a bug while testing, don't just make a "quick fix" -- go back and understand precisely why it didn't work, and understand how your intended fix will modify all behaviors, not simply the one that's currently misbehaving.
If you think about it carefully you can make it work with a minimum of problems. If you don't think carefully you'll be chasing your tail for hours.

Categories

Resources