Linked List creation

This is a discussion on Linked List creation within the C Programming forums, part of the General Programming Boards category; Hi - I'm just trying to create a cery simple function to create a linked list of a given amount ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Linked List creation

    Hi -

    I'm just trying to create a cery simple function to create a linked list of a given amount of nodes. If that makes sense.

    So the number of required nodes is passed to the function
    The function then allocates enough memory for the linked list
    The list is passed back from whence it was called from.

    I have already attempted this but have come a cropper so just need a fresh perspective on it if anyone has one?

    Thanks for any help

    Helen

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by thedoofus

    So the number of required nodes is passed to the function
    The function then allocates enough memory for the linked list
    The list is passed back from whence it was called from.
    I wouldn't recomend allocating all of the list's memory in one chunk, each node should have it's memory allocated seperatly so that you can remove nodes properly.
    First you would create the root Node then too add a node to the list you would allocate memory the size of one node and save the pointer to that node in the root node's Next Pointer. Then move to the next node using the next pointer and allocate the next node and so on...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do this in one function. It's really easy:
    Code:
    List *makelist( datatype array[], int size )
    {
        for x = 0; x < size; x ++
            allocate one node
            fill this node with array element x's data
            link this node to a list 
    
        return the first element in the list
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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