nodes

This is a discussion on nodes within the C++ Programming forums, part of the General Programming Boards category; well can someone exsplan node and i did read the tutoreal about 5 times... but i dont get it... it ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    Post 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

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    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.
    Last edited by JoshR; 07-01-2005 at 04:35 PM.

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    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.
    Last edited by JoshR; 07-01-2005 at 04:37 PM.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    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
    Last edited by JoshR; 07-01-2005 at 04:48 PM.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    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
    Last edited by JoshR; 07-01-2005 at 05:06 PM.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    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.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    A graph G is a set of vertex (nodes) v connected by edges (links) e. Thus G=(v , e).

    Vertex (Node). A node v is a terminal point or an intersection point of a graph. It is the abstraction of a location such as a city, an administrative division, a road intersection or a transport terminal (stations, terminuses, harbors and airports).

    Edge (Link). An edge e is a link between two nodes. The link (i , j) is of initial extremity i and of terminal extremity j. A link is the abstraction of a transport infrastructure supporting movements between nodes. It has a direction that is commonly represented as an arrow. When an arrow is not used, it is assumed the link is bi-directional.

    source: http://people.hofstra.edu/geotrans/e...n/ch2m1en.html
    signature under construction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programmatically creating nodes in a treeview problem
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 10-12-2006, 02:41 PM
  2. interchange nodes in a linked list
    By Gustavo in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2004, 07:14 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. counting nodes
    By sballew in forum C Programming
    Replies: 8
    Last Post: 10-27-2001, 08:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21