# nodes

• 07-01-2005
firefly
nodes
well can someone exsplan node and i did read the tutoreal about 5 times... but i dont get it... it usually helps me when i can see two examples so i can compare.... pls someone
• 07-01-2005
JoshR
Nodes are often used to store data.

Generaly you use a node in a linked list. Each node contains data and a pointer to the next node. Think of a node as a container that holds your data and has a label that has the location for the next node.

A node is something that connects everything together. A linked list can have nodes that are connected together.

For example:
Code:

```struct Node {   int data;   Node* nextNode; };```
Observe that the node contains data and a pointer to the next node.
Sometimes you will see a binary tree that has a node with two pointers, one to the left and one to the right. In essence all the data is still connected, but in a different manner.
• 07-01-2005
JoshR
Some example code that uses nodes can be as simple as:
Code:

```#include <iostream> using namespace std; struct node {   int data;   node* next; }; int main () {   node one;  //our first data container   node two;  //our second data container that will be pointed to by one   one.data = 1;  //our int will be one obviously, but data can be anything you want it to be   one.next = &two;  //we will have one point to two   two.data = 2;   two.next = NULL;  //two is the last item linked so it doesnt need to point anywhere   //now to access the data we can simply do   node* temp = &one;   while (temp != NULL) {       cout << temp->data << endl;       temp = temp->next;   }     cin.get();   return 0; }```
In conclusion, Nodes are used to store data in a "linked" manner. Each node contains data and a pointer to the next node.
• 07-01-2005
JoshR
Code:

```Node 1        Node 2        Node 3 +------+      +------+      +------+ | data |  ->  | data |  ->  | data | +------+      +------+      +------+ // -> == pointer // data can be whatever you want, any type // nodes can point to any node, it doesnt have to be in order```
• 07-01-2005
JoshR
Just to make sure its clear...

A Node holds data, just like an array can hold data.

In an array you have random access, meaning you can simply gain access to a value at any random location on the array.

So if I had an array:
Code:

`int array[3] = {0, 1, 5};`
Then I can randomly access any number:
Code:

```cout << array[2] << endl; cout << array[0] << endl; cout << array[1] << endl;```
The way I access it doesnt have to be incremental. But if I were to use nodes, than I have to start at a node and can only go to the next node and so forth.
Code:

```// pseudocode: Make 3 nodes connected together with data in each Make a temp node pointer   point temp to the first node // to get to the second node: change temp to temp->next so that temp is positioned at the 2nd node now output the data from temp```
• 07-01-2005
JoshR
You may not see usefullness in using nodes at this point, but once you get into more complex algorithms that involve dynamic memory allocation (meaning memory depends on the data during runtime, usually on input) you will find that it can become very usefull.

If used correctly Nodes can be linked and shifted around without using extra memory.
• 07-01-2005
DougDbug
A node is a specific place in a connected structure. It could be the 'Y' joint between branches. It could be an end-point. It could be an element between two other elements.

A road is not a node, but n intersection is a node, a “fork” in the road is a node. A house on the street is a node. A “dead end” is a node.

Expanding on what Josh said… when you think about nodes, you are generally working with relative positions, rather than absolute addresses…
Where does Doug live? 2 houses from the end of the street. Where does Josh live? 3 houses down from Doug.
Where does firefly live? Across the street from Josh.

A little closer to the programming world...
Imagine that you are changing all of the characters in a sentence to upper-case (by hand). You wouldn’t be thinking 1st character, 2nd character, 3rd character… 13th character… You would be thinking 1st character, next-character, next-character, next-character, next-character… last character. In this iterative process, you don't need to know exactly where you are… just where to start, which direction to go, and when to stop.
• 07-01-2005
major_small
if you need more code as an example, take a look at the code I submitted for ilovevector's contest #3. the code is in one of ilovevector's posts further down the page (it may even be on a second/third page for you - I have it set to board max posts/page)
• 07-02-2005