Thread: anyone have linked-list sample codes?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Unhappy anyone have linked-list sample codes?

    Any one have some simple sample codes for Linked-List codes? because from the examples I found from books, they mostly only declare integer data inside the node.

    What I concern is, I need an example which have declare a character (e.g : char name[30]) data inside the node.

    Thank you so much.


  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    I don't really understand why the data type of the node would effect your understanding of the data structure. Is there a more specific issue you need help with?

    P.S There are many many examples of linked lists found all over the web.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Ah the beauty of STL:

    Code:
    #include <list>     // For the list object
    #include <cstring>  // For strncpy
    #include <iostream> // For endl and cout
    
    using namespace std;
    
    struct node
    {
        char data[30];
        node( char* info ) // Constructor for node struct
        {
            strncpy( data, info, 30 );
        };
    };
    
    int main()
    {
        list<node> NodeList;            // Linked list of nodes
        list<node>::iterator itList;    // To loop though the nodes
    
        // Build a linked list of nodes with some character strings.
    
        NodeList.push_back( node("Hello, today") );
        NodeList.push_back( node("is the") );
        NodeList.push_back( node("3rd of July.") );
    
        // Go through the linked list and output the data in the nodes.
    
        for( itList = NodeList.begin(); itList != NodeList.end(); ++itList )
            cout << (*itList).data << endl;
    
        return 0;
    }
    I tested it after typing it in just as you see it above and there were no errors under MSVC++ 6.0. You could probably use #include <string> and use string objects instead of the character arrays.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Unregistered
    Guest
    STL is nice if you don't feel the need to understand what is going on under the behind the scenes.

    Basically a list is a structure to hold data. The list can contain a group of nodes (sometimes called links). Each node has two separate parts. One part is the address of the next node in the list. The other part is data. Data can be anything. It can be a single int, a single char, a single string, or a complex set of data. Each node will have the same data format format however. As with other structures, like arrays, just because the structure exists, doesn't mean that there is anything stored in it. That is the list can be empty.

    To add a node to the list you must first declare memory for the node. This is usually done dynamically, but it doesn't have to be. Data is then assigned to the various data members of the node either before or after adding it to the list. Use whatever assignment strategies are necessary depending on the type of the data member(s).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM