Hello, I was reading a book and it says that when we want to add new Node that is not in the Head we have make a selection i.e. selection.addNodAfter(element), before we initiate our method. The question is where do i have to do this "selection"? in my main or how else do i implement it?
Thank you

Hello grisha83,
The question is not clear. Where do you want to add a node?
and which language are you using? ..
i have this strange voices in my head telling me you are 'talking about Javascript'?

Regards,
SNIP

Oops sorry,
I am using Java. Also, im not sure where i am using it. I was just reading my textbook and the author has noted that i have to select a node after which i want to input new node.

What? Be more specific and explain your problem fully

Oops sorry,
I am using Java. Also, im not sure where i am using it. I was just reading my textbook and the author has noted that i have to select a node after which i want to input new node.

Mr. Author says that "Where you want to add a node?" So, you have to select position of node or a node.

Ok,
here is the thing its not a specific problem its just a concept but before i implement it i have to know how.
Let me try to create a problem so you can visualize what i don't understand:
lets say for instance i have 4 nodes 1st with value 10 that links to the 2nd with value 20 that links to the 3rd node with value 42 that links to the 4th node with value 30 that links to null(its a tail node)
so the book says if i want to remove node with value 42(3rd node) i have to set up a selection first and then implement a remove method:
...Activating the following method removes the node after the seleced node:
selection.removeNodeAfter();
implementation of removeNodeAfter needs only one statement t accomplish its work:
link = link.link;
so i am not even sure myself what they mean exactly

Ok, now I understand. Imagine a Node that has two attributes. The first attribute will be the value, which is an Integer. The second attribute will be another Node. (This Node will be the 'next' node, which is what they are calling the link). So basically if you had four nodes, containing: 1 2 3 4, (I'm just going to call the Nodes node1, node2, node3, and node4). So the first Node would be '1', and it would have a link to node2. The Node that contains 2 would have a link to node3. So now lets say that we want to remove a particular Node, node3. Your book is saying that you would need to find the Node before node3. In this case, that is node2.


Your book is saying that in order to remove node3, you would need to use node2.removeNodeAfter(). The code for removeNodeAfter() would look like this:

public void removeNodeAfter(){
link = link.link;
}

Explanation: Since we called the method with node2.removeNodeAfter(), "link" is the variable from node2. Remember that "link" contains the next Node. In this case, the next node is node3, so link is node3. Then link.link is the same as saying node3.link, which is node4. So saying link = link.link; is making node2 point to node4. Essentially, we have just deleted nodeThatContains3.

If that explanation didn't make sense, keep this in mind: In my example, I called the method with node2. So "link" refers to node3. And link.link refers to node3.link, which is node4. So we are setting node2's link to node4-- essentially deleting nodeThatContains3 from our linked list of Nodes. It might seem confusing since you might say to yourself "where is it being deleted"? But it doesn't need to be deleted. . just removed from the list. Now when you go through each Node, node1.link = node2, and node2.link = node4. Java's automatic garbage collection will handle actually deleting node3.

Hope that helps :)

It kind of makes sense. I guess my problem is not being able to imagine the design and implementation part of it. Sorry for jumping from one thing to another but i have read the chapter through and they have an exercise where i have to add new node and make it a second node. In case if there are no nodes i have to add it as new node. So here is my code. When i print my method output i get "test: IntNode @10d448", which tells me that i use wrong type of variable(passing improper data type) Am i correct?.
Anyway here is my code:

public class Main {

  
    public static void main(String[] args) {

        IntNode myint = new IntNode(20,null);
        //selection.addNewNode(3);

        myint.addNewNode(3, null);
        System.out.println("test:" + myint.addNewNode(3, null));
    }

}
public class IntNode {
    private int data;
    private IntNode link;

    public IntNode(int initialdata, IntNode initiallink){
        data =  initialdata;
        link = initiallink;
    }

    public IntNode addNewNode(int element, IntNode link){
        if (link !=null){
            link = new IntNode(element,link);
        }
        else{
            link = new IntNode(element,link);
        }
        return link;
    }

}

Actually, please disregard my last reply. What i couldn't understand was how to actually create LinkedLists and Nodes. Apparently i have to create one class Node, and another class LinkedList then i have to initialize both and only then i can implement addition and removal methods.
Thank you

All of this is do-able in one class. But I've seen the texts before, they usually do it using two classes. Good luck & mark this as solved if your question was answered

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.