Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dynamic list of structures

Reply
Thread Tools

dynamic list of structures

 
 
irwa82
Guest
Posts: n/a
 
      02-13-2009
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;
}

 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      02-13-2009

"irwa82" <no-> 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[i].next = & sarray[i+1];
sarray[SIZE-1].next = NULL; /* Already NULL but just in case..
*/

for (i=0; i<SIZE; ++i)
sarray[i].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

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      02-13-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Managing Dynamic Data Structures with Lightweight Database Neo C Programming 1 08-02-2007 09:02 PM
Dynamic Structures googlinggoogler@hotmail.com C++ 12 08-29-2006 10:26 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
"Dynamic" Object Structures? Randy Yates C++ 3 12-30-2004 04:27 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57