# Understanding Nodes

Page 1 of 1

## 4 Replies - 2794 Views - Last Post: 07 December 2009 - 06:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144088&amp;s=44b2559a1be7044a5aae818e417a33d4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tscott

• D.I.C Regular

Reputation: 2
• Posts: 370
• Joined: 30-January 09

# Understanding Nodes

Posted 07 December 2009 - 05:11 PM

Anyone have any good practice problems or sites that break down nodes well? I'm in my 2nd java class I tried googling for examples and got nothing but advanced shit anyhow I need to know how to transverse and remove them as well as having a general understanding of how they work.

I mean I know that if you wanted to add a node after another u'd need to know the address of the node and that there is basically 2 parts to a know the value and the .next part but thats about all I understand right now and the book is terrible

Is This A Good Question/Topic? 0

## Replies To: Understanding Nodes

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,395
• Joined: 27-December 08

## Re: Understanding Nodes

Posted 07 December 2009 - 05:51 PM

A Node is fairly simple- it holds an element and it points to the next Node, which is useful when traversing the LinkedList. Think about it like this- there are a group of people in line. Each person points to the next person in line. If there isn't another person in line, then the end person points to nobody. Like so:
```A --> B --> C --> D -->

```

So to traverse, start at the head of the line, person A. Does A point to another person? Yes, go to that person, labeled B. Does B point to another person? Yes, go to C. And so on.

When working with adding or removing elements from the middle of the list, it is just a matter of re-arranging the pointers. For example, if you want to remove person B from the List, you would want A to point to C. So in terms of Nodes, you would modify A.next = A.next.next. Does this make sense?

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Understanding Nodes

Posted 07 December 2009 - 05:52 PM

Nodes always have a forward link which is a pointer to the next node
this forward link might be null if you are a the last node in the list

you start by the first node and go up to the next node using the forward link and to the next one.... and so on until the forward link is null
when to find that last node with the null forward link you set this forward link to point to the node you want to add
and you set to null the forward link of the node you add

to remove node N
you set the forward link of node N-1 to the forward link of node N (which might be null if the node N was the last node in the list

hope it is clearer

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Understanding Nodes

Posted 07 December 2009 - 05:55 PM

### #5 vasdueva

Reputation: 15
• Posts: 141
• Joined: 03-April 07

## Re: Understanding Nodes

Posted 07 December 2009 - 06:30 PM

Everything in Java(except primitives) are actually pointers under the hood.

Pointers do exactly what it sounds like they should do, they 'point' to some data.
As long as you understand the concept of pointers, Nodes are a simple concept.

A node contains a pointer and an object(or multiple objects), the pointer points to another Node.(or if the last Node probably NULL)
So imagine you start at the beginning and want to get to a specific Node. You'd first go to where the pointer in the current Node
points(another node), look at the Ojbect in that Node or move to where the pointer of that Node points.

Here's a singly linked Node like structure. Since all Objects in Java are pointers, the field, pointer, of the Node
class is actually a pointer to another Node.
```public class Node{
private Node pointer;
private Object data;
public Node(Node nextNode, Object data) {
pointer = nextNode;
this.data = data;
}

public Node getNextNode() {
return pointer;
}
public Object getData() {
return data;
}
}

```