dynamic list of structures

Discussion in 'C Programming' started by irwa82, Feb 13, 2009.

  1. irwa82

    irwa82 Guest

    Hi,

    I am new to C and created a list of structures linked together with
    pointers. But I am wondering how you create a list if you don't have a
    fixed number of entries. like if you had an address book you may not
    know how many entries are there etc.

    here is some sample code of my fix sized list:

    #include <stdio.h>
    #include <stddef.h>

    int main(void) {
    struct sTest {
    int number;
    struct sTest *next;
    } s1, s2, s3, s4, s5, *sPointer;

    sPointer = &s1;

    s1.next = &s2;
    s2.next = &s3;
    s3.next = &s4;
    s4.next = &s5;
    s5.next = (struct sTest *) NULL;

    s1.number = 15;
    s2.number = 25;
    s3.number = 35;
    s4.number = 45;
    s5.number = 55;

    while (sPointer != (struct sTest *) NULL) {
    printf("%i\n", sPointer->number);
    sPointer = sPointer->next;
    }

    return 0;
    }
    irwa82, Feb 13, 2009
    #1
    1. Advertising

  2. irwa82

    Bartc Guest

    "irwa82" <> wrote in message
    news:01a4bb59$0$20644$...
    > Hi,
    >
    > I am new to C and created a list of structures linked together with
    > pointers. But I am wondering how you create a list if you don't have a
    > fixed number of entries. like if you had an address book you may not
    > know how many entries are there etc.
    >
    > here is some sample code of my fix sized list:
    >
    > #include <stdio.h>
    > #include <stddef.h>
    >
    > int main(void) {
    > struct sTest {
    > int number;
    > struct sTest *next;
    > } s1, s2, s3, s4, s5, *sPointer;
    >
    > sPointer = &s1;
    >
    > s1.next = &s2;
    > s2.next = &s3;
    > s3.next = &s4;
    > s4.next = &s5;
    > s5.next = (struct sTest *) NULL;
    >
    > s1.number = 15;
    > s2.number = 25;
    > s3.number = 35;
    > s4.number = 45;
    > s5.number = 55;
    >
    > while (sPointer != (struct sTest *) NULL) {
    > printf("%i\n", sPointer->number);
    > sPointer = sPointer->next;
    > }
    >
    > return 0;
    > }


    Your code is not easily scaleable (imagine 5000 nodes). Proper linked list
    code has already been posted, but it might be useful to look at code only
    slightly modified from yours. The following uses an array as storage for the
    nodes, instead of naming individual nodes:

    #define SIZE 5

    int main(void) {
    struct sTest {
    int number;
    struct sTest *next;
    } sarray[SIZE]={0}, *sPointer;
    int i;

    sPointer = &sarray[0];

    for (i=0; i<SIZE-1; ++i)
    sarray.next = & sarray[i+1];
    sarray[SIZE-1].next = NULL; /* Already NULL but just in case..
    */

    for (i=0; i<SIZE; ++i)
    sarray.number = i*10+15;

    while (sPointer != (struct sTest *) NULL) {
    printf("%i\n", sPointer->number);
    sPointer = sPointer->next;
    }
    return 0;
    }

    Now you can change the number of nodes very easily. The SIZE nodes are
    allocated here consecutively in elements 0 to SIZE-1 of the array, but could
    be in any order.

    --
    Bartc
    Bartc, Feb 13, 2009
    #2
    1. Advertising

  3. irwa82

    CBFalconer Guest

    irwa82 wrote:
    >
    > I am new to C and created a list of structures linked together
    > with pointers. But I am wondering how you create a list if you
    > don't have a fixed number of entries. like if you had an address
    > book you may not know how many entries are there etc.


    You create a node:

    typedef struct node {
    struct node *next;
    void *data;
    } *node;

    and an empty list:

    node list = NULL;

    Now you can write all your list manipulation code, not caring about
    what the data pointer holds. The addition to the list will malloc
    a suitable pointer for the data, copy the data into there, malloc a
    "struct node", and set the *data pointer. The code to insert that
    into the list will do:

    node addtolisthead(void *new) {
    node p;

    if (!(p = malloc(sizeof *p))) return NULL; /* failure */
    p->next = list;
    p->data = new;
    list = p;
    return p; /* non-NULL, success */
    }

    Note how easily you can alter this so that list is no longer a
    global. Note that addtolisthead doesn't need to know anything
    about the data.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Feb 13, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy Yates

    "Dynamic" Object Structures?

    Randy Yates, Dec 29, 2004, in forum: C++
    Replies:
    3
    Views:
    356
    Randy Yates
    Dec 30, 2004
  2. tweak
    Replies:
    14
    Views:
    2,769
    Eric Sosman
    Jun 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
  4. Dynamic Structures

    , Aug 29, 2006, in forum: C++
    Replies:
    12
    Views:
    553
    Victor Bazarov
    Aug 29, 2006
  5. Neo
    Replies:
    1
    Views:
    357
    CBFalconer
    Aug 2, 2007
Loading...

Share This Page