-5

I have added the insertion function and display could anyone help me with the deletion That would be much appreciated.

public class Node 
{
   Object data;
   Node next;
}

public class LkList {
    public Node insertVar(Object var1, Node ls1) {
        Node p = new Node();
        p.data = var1;
        p.next = ls1;
        ls1 = p;
        return ls1;
    }

    public Node deleteVar(Object var1, Node ls1){
        // deletion function goes here
    }

    public void printL(Node ls1) {
        System.out.print("The lklist of variables is: ");

        Node p = ls1;

        while (p != null) {
            System.out.print("[" + p.data + "]" + "->");
            p = p.next;
        }
        System.out.println();
    }
}
9

2 Answers 2

1

Deletion of a Node will be as just removing its reference accessibility. In your case, you want to delete the node by comparing the given values of the Variable. So starting from the head node you have to compare each node's data (specific speaking it Variable Data). You have to compare the two Node's data and then simply break then simply apply the link list deletion logic. Only the main concern here is to compare the two nodes data. Don't compare the node by using

.equals() method

    public Node deleteVar(Object var1, Node ls1) {
    if (ls1.data.varName.equals(var1.varName) && ls1.data.varValue.equals(var1.varValue)) { // If it is the first node which is to be deleted
        return ls1.next;
    }
    else {
        Node prev = ls1;

        while (prev.next != null) {
            if (prev.next.data.varName.equals(var1.varName) && prev.next.data.varValue.equals(var1.varValue)) {
                // skip the node by linking the previous one to the next one directly
                prev.next = prev.next.next;
                break;
            }
        }

        return ls1;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you copy/paste my answer instead of editing it, you should at least write code that would properly compile. I have used Object for the data/variable, hence there is no name and value. I used equals to simplify the code as it makes it easier to concentrate on the linkedList logic. On top of that, I don't even know what equals should mean (just name or name and value?). Variable should define that in its equals method instead of writing it hard-wired in the linkedList.
Sorry forgive me. I wasn't aware of the above facts. And I will make corrections in my code as soon as possible
-1

From your code and your explanation I assume that ls1 is the first node of your list and you want to remove the first node in the list that has var1 as its data:

public Node deleteVar(Object var1, Node ls1)
{
    if (ls1.data.equals(var1))
    {
        //simply pop the first node
        return ls1.next;
    }
    else
    {
        Node prev = ls1;

        while (prev.next != null)
        {
            if (prev.next.data.equals(var1))
            {
                // skip the node by linking the previous one to the next one directly
                prev.next = prev.next.next;
                break;
            }
        }

        return ls1;
    }
}

12 Comments

well thank you very much ... really appreciate it..
You are welcome. For the next question you ask, try to be more concise. Don't know if you have seen that I have edited your question to remove unnecessary parts of your code (well, unnecessary for this question at least). It makes understanding what you ask much easier if you reduce your code snippets to the minimum. Also you might want to check the question I linked to in the comment under your question. The answers there are more detailed, and deal with null for example.
but in my code you got menu so when you choose 1 you can insert values with their variable .. lets say you run the code it asks 1- insert 2-display 3-delete 4-exit... and lets say you choose 1 insertion so it asks first to insert the variate(a) and then the value(2) etc.. however, when you want to delete it asks first to input the variable and then the value... im not good at explaining hope it helps
this is my first time on this web
Sure, maybe you could leave that part in, but I think after deleting that middle part (the class with the weird name) and the Menu, the code is pretty self explanatory. Just re-name LkList to LinkedList, and everybody will immediately understand what you need. It depends on each question how much code you need to show. Hope I could help. (Also, if you agree with the edits I made, you can accept (or improve) them to make them visible for the next reader.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.