473,480 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Nesting Linked Lists?

Hello Friends,

I would like to implement a linked list in C that can be re-used to
store various datatypes. In the past, I've declared something like:

typedef TYPE type_of_data_in_list ;

at the top of my header (where type_of_data_in_list was something like
int, char, etc..) and things always worked well. I am now wondering
how I should go about storing a list...which contains a list. For
example, if I have a struct A which contains a List of chars, i would
simply start off my file with typedef TYPE char; But what do I do if I
want to create a List of struct A's? I can't simply add typedef TYPE
struct A ; to the file because that would get rid of the reference a
depends on internally?

Hopefully someone understands what I'm saying and can point me to a
solution.

Thank you in advance,
Mike.

Dec 1 '06 #1
3 2522
Hello Mike,

lists of lists etc. are easy with those macros:
http://www.openbsd.org/cgi-bin/man.cgi?query=queue

contained in this header (which you could copy into you program):
http://www.openbsd.org/cgi-bin/cvswe...eue.h?rev=1.31

(and you can debug with -DQUEUE_MACRO_DEBUG ).

Smth. like (untested):

typedef struct list {
TAILQ_ENTRY(list) entry;
int data;
} list;

typedef struct lol {
TAILQ_ENTRY(lol) entry;
TAILQ_HEAD(, list) head;
} lol;

Regards
Alex

--
http://preferans.de

Dec 1 '06 #2
Le 01-12-2006, Cyron <md*******@yahoo.coma écrit*:
I would like to implement a linked list in C that can be re-used to
store various datatypes. In the past, I've declared something like:

typedef TYPE type_of_data_in_list ;

at the top of my header (where type_of_data_in_list was something like
int, char, etc..) and things always worked well. I am now wondering
how I should go about storing a list...which contains a list. For
example, if I have a struct A which contains a List of chars, i would
simply start off my file with typedef TYPE char; But what do I do if I
want to create a List of struct A's? I can't simply add typedef TYPE
struct A ; to the file because that would get rid of the reference a
depends on internally?

Hopefully someone understands what I'm saying and can point me to a
solution.
I know two kinds of solutions:
1) first is based on void*, that is to say you write a list that
handle pointers on any type (but you loose type checking)
2) second is to generates macro names

You can have a look at my BPL librairy that use the 2d solution:
http://www.enseeiht.fr/~boyer/Tools.html (any feedback welcomed)

Marc Boyer
Dec 1 '06 #3
Cyron wrote:
I am now wondering
how I should go about storing a list...which contains a list.
/* BEGIN listolists.c output */

Man number 1
Initials: O.
Age : 32
Phone numbers:
163-4945
434-9890
785-6541
432-9672

Man number 2
Initials: B.L.
Age : 36
Phone numbers:
945-4349
890-7856
541-4329
672-3216

Man number 3
Initials: J.
Age : 24
Phone numbers:
349-8907

Man number 4
Initials: S.F.G.
Age : 24
Phone numbers:
907-8565
414-3296
723-2165
238-3036
381-0527

Man number 5
Initials: F.G.
Age : 20
Phone numbers:
565-4143
296-7232
165-2383

/* END listolists.c output */
/* BEGIN list_of_lists.c */

#include <stdio.h>
#include <stdlib.h>

#define PEOPLE_MAX 5
#define NAME_LENGTH 3
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069LU + 362437 & 0xffffffff)

struct list_node {
struct list_node *next;
void *data;
};

typedef struct list_node list_type;

struct person {
char name[2 * NAME_LENGTH + 1];
int age;
list_type *phone_numbers;
};

list_type *list_make(long unsigned count);
list_type *man_init(list_type *node, long unsigned seed);
list_type *number_init(list_type *node, long unsigned seed);
void make_name(char *name, size_t length, long unsigned seed);
int man_print(list_type *node);
int number_print(list_type *node);
void list_free(list_type *node, void (*free_data)(void *));
void person_free(void *data);

int main(void)
{
list_type *head;
long unsigned seed = LU_RAND_SEED;

puts("/* BEGIN listolists.c output */");
head = list_make(1 + seed % PEOPLE_MAX);
if (head == NULL) {
puts("No list");
}
seed = LU_RAND(seed);
if (man_init(head, seed) != NULL) {
man_print(head);
}
list_free(head, person_free);
puts("\n/* END listolists.c output */");
return 0;
}

list_type *list_make(long unsigned count)
{
list_type *node, *list;

list = count != 0 ? malloc(sizeof *list) : NULL;
if (list != NULL) {
node = list;
while (--count != 0) {
node -data = NULL;
node -next = malloc(sizeof *node -next);
if (node -next == NULL) {
list_free(list, free);
return NULL;
} else {
node = node -next;
}
}
node -data = NULL;
node -next = NULL;
}
return list;
}

list_type *man_init(list_type *node, long unsigned seed)
{
list_type *head;
struct person man;

head = node;
while (node != NULL) {
node -data = malloc(sizeof man);
if (node -data == NULL) {
puts("man_init malloc problem");
head = NULL;
break;
}
seed = LU_RAND(seed);
make_name(man.name, NAME_LENGTH, seed);
seed = LU_RAND(seed);
man.age = 20 + seed % 20;
seed = LU_RAND(seed);
man.phone_numbers = list_make(1 + seed % PEOPLE_MAX);
if (man.phone_numbers == NULL) {
puts("man.phone_numbers == NULL");
head = NULL;
break;
}
seed = LU_RAND(seed);
if (number_init(man.phone_numbers, seed) == NULL) {
puts("man_init(man.phone_numbers, seed) == NULL");
head = NULL;
break;
}
*(struct person *)node -data = man;
node = node -next;
}
return head;
}

list_type *number_init(list_type *node, long unsigned seed)
{
list_type *head;
size_t count;
char *string;

head = node;
while (node != NULL) {
node -data = malloc(sizeof "xxx-xxxx");
if (node -data == NULL) {
puts("number_init malloc problem.");
head = NULL;
break;
}
string = node -data;
count = sizeof "xxx-xxxx" - 1;
while (count-- != sizeof "-xxxx" - 1) {
seed = LU_RAND(seed);
*string++ = (char)('0' + seed % 10);
}
*string++ = '-';
while (count-- != 0) {
seed = LU_RAND(seed);
*string++ = (char)('0' + seed % 10);
}
*string = '\0';
node = node -next;
}
return head;
}

int man_print(list_type *node)
{
long unsigned count;
struct person *man;
int rc;

for (count = 1; node != NULL; ++count) {
man = node -data;
rc = printf
("\nMan number %lu\n"
"Initials: %s\n"
"Age : %d\nPhone numbers:\n",
count, man -name, man -age);
if (EOF == rc) {
break;
}
number_print(man -phone_numbers);
node = node -next;
}
return node == NULL ? 1 : EOF;
}

int number_print(list_type *node)
{
while (node != NULL
&& printf(" %s\n", (char *)node -data) != EOF)
{
node = node -next;
}
return node == NULL ? 1 : EOF;
}

void make_name(char *name, size_t length, long unsigned seed)
{
length -= seed % length ;
while (length-- != 0) {
seed = LU_RAND(seed);
*name++ = UPPER[seed % sizeof UPPER];
*name++ = '.';
}
*name = '\0';
}

void list_free(list_type *node, void (*free_data)(void *))
{
list_type *next_node;

while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}

void person_free(void *data)
{
list_free(((struct person *)data) -phone_numbers, free);
free(data);
}

/* END list_of_lists.c */

--
pete
Dec 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4798
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
3
2243
by: lawrence | last post by:
This is a follow up question to the conversation that started here: http://groups.google.com/groups?hl=en&lr=&safe=off&selm=da7e68e8.0410010901.18a813c9%40posting.google.com I tried nesting...
10
15095
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
12818
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
12
2490
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of...
3
3252
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
4
3582
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
12
3926
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
8554
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.