473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested linklist

I don't really know how to say it so I just say it a nested linklist.

How do you make LinkLists inside LinkList?
Can anyone help me for this?
I think an example program will help me a lot.

thank you.

Nov 14 '05 #1
6 2176


jwvai316 wrote:
I don't really know how to say it so I just say it a nested linklist.

How do you make LinkLists inside LinkList?
Can anyone help me for this?
I think an example program will help me a lot.


Let's build this from the ground up. Suppose you have
a struct containing some kind of "payload" -- a pair of
`double' values, let's say -- and you wish to put a bunch
of such structs into a linked list. One common way to do
this is to augment the struct with a `next' pointer:

struct node { /* a node in a list: */
double x, y; /* payload data */
struct node *next; /* next node in list */
};

So far, so good. You may also want to define another
kind of struct that keeps track of useful information about
a linked list -- pointers to the first and last nodes, maybe
some other data:

struct list { /* info about an entire list: */
struct node *head; /* pointer to first node */
struct node *tail; /* pointer to final node */
unsigned int length; /* number of nodes in list */
};

So far, we've started from a struct with some payload
data and built up the machinery to hold such structs in a
list: we added a link to each struct, and we defined a new
struct type to manage the list as a whole. (The examples
above are illustrations; linked lists need not necessarily
be implemented in exactly this way. Adjust your definitions
to match your needs.)

All right, now you want to form a sort of super-list whose
individual nodes are linked lists as above. How? By repeating
the exact same operations that took you from an x-and-y pair to
a linked list, only this time treating the `struct list' as
the payload for the super-list. The first step is to add a
`next' pointer along with the payload:

struct list { /* info about an entire list: */
struct node *head; /* pointer to first node */
struct node *tail; /* pointer to final node */
unsigned int length; /* number of nodes in list */
struct list *next; /* next list in super-list */
};

This is a node that can be part of a linked list (thanks to its
own `next' pointer) and whose payload happens to describe a
linked list containing another style of node. If you like, you
can define another struct type to keep track of this super-list:

struct super_list { /* info about an entire super-list: */
struct list *head; /* first list in super-list */
struct list *tail; /* final list in super-list */
};

You could continue in this manner, building more and more
levels: equip a `struct super_list' with its own `next' pointer,
define a new `struct super_duper_lis t' to keep track of a list
of `struct super_list's, and so on. However, things will soon
become rather unwieldy if you pursue this too far: I'd suggest
that if you find yourself using more than two levels as shown
here you might want to consider different data structures like
trees.

Once again, the examples above are just illustrations. The
name "linked list" applies to more than just the singly-linked
structures I've shown; you might use double linkage (a `prev'
along with each `next' link), circular linkage (the tail node
points back to the head instead of to NULL), a list that
operates as a stack, or as a queue, or as a general deque,
sorted lists, priority lists, ... The main point is that if
you have a "list-describing structure" of some kind, then you
can make that structure be the payload for a super-list in just
the same way that the list's own nodes became part of the list.

--
Er*********@sun .com

Nov 14 '05 #2
jwvai316 wrote:

I don't really know how to say it so I just say it a nested linklist.

How do you make LinkLists inside LinkList?
Can anyone help me for this?
I think an example program will help me a lot.


Lets deal with a general purpose node:

struct node {
struct node *next;
struct node *sublist;
whatever *data;
}

so we can use *data to attach any sort of data to any node. Now we
can tie them together something like this:

(NULL) (NULL) (NULL)
^ ^ ^
| | |
NODE -----> NODE -----> NODE -->(NULL)
^
| (NULL) (NULL) (NULL)
| ^ ^ ^
| | | |
NODE -----> NODE -----> NODE -----> NODE -->(NULL)
^
| (NULL)
| ^
| |
NODE -----> NODE
^
| (NULL) (NULL) (NULL)
| ^ ^ ^
| | | |
NODE -----> NODE -----> NODE -----> NODE -->(NULL)
^
| (NULL) (NULL) (NULL)
| ^ ^ ^
| | | |
NODE -----> NODE -----> NODE -----> NODE -->(NULL)
^
|
root

where we can cut things off towards the right and top. The upwards
pointing arrows represent the field 'next', and the right pointing
arrows represent the field 'sublist'. Many of those pointers are
NULL, and we may be able to find another use for those if we add
some sort of descriptor field to the node. Meanwhile we can
preserve access to the whole thing by a single pointer in root (of
type struct node *).

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #3
jwvai316 wrote:

I don't really know how to say it so I just say it a nested linklist.

How do you make LinkLists inside LinkList?
Can anyone help me for this?
I think an example program will help me a lot.


/* BEGIN listolists.c output */

Man number 1
Name: Iofo
Age : 32
Phone numbers:
163-4945
434-9890
785-6541
432-9672

Man number 2
Name: Eblf
Age : 36
Phone numbers:
945-4349
890-7856
541-4329
672-3216

Man number 3
Name: Fjut
Age : 24
Phone numbers:
349-8907

Man number 4
Name: Vsfg
Age : 24
Phone numbers:
907-8565
414-3296
723-2165
238-3036
381-0527

Man number 5
Name: Wfgn
Age : 20
Phone numbers:
565-4143
296-7232
165-2383

/* END listolists.c output */

/* BEGIN listolists.c */

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

#define PEOPLE_MAX 5
#define NUMBERS_MAX 3
#define NAME_LENGTH 5
#define UPPER "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
#define LOWER "abcdefghijklmn opqrstuvwxyz"
#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[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(li st_type *node, long unsigned seed);
void make_name(char *name, size_t length, long unsigned seed);
void man_print(list_ type *node);
void number_print(li st_type *node);
void list_free(list_ type *node, void (*free_data)(vo id *));
void person_free(voi d *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) {
fputs("No list\n", stderr);
}
seed = LU_RAND(seed);
head = man_init(head, seed);
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) {
fputs("man_init malloc problem\n", stderr);
head = NULL;
break;
}
seed = LU_RAND(seed);
make_name(man.n ame, NAME_LENGTH, seed);
seed = LU_RAND(seed);
man.age = 20 + seed % 20;
seed = LU_RAND(seed);
man.phone_numbe rs = list_make(1 + seed % PEOPLE_MAX);
if (man.phone_numb ers == NULL) {
fputs("man.phon e_numbers == NULL\n", stderr);
head = NULL;
break;
}
seed = LU_RAND(seed);
if (number_init(ma n.phone_numbers , seed) == NULL) {
fputs("man_init (man.phone_numb ers, seed) == NULL\n",
stderr);
head = NULL;
break;
}
*(struct person *)node -> data = man;
node = node -> next;
}
return head;
}

list_type *number_init(li st_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) {
fputs("number_i nit malloc problem\n", stderr);
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;
}

void man_print(list_ type *node)
{
long unsigned count;
struct person *man;

for (count = 1; node != NULL; ++count) {
man = node -> data;
printf(
"\n%5c Man number %lu\n"
"Name: %s\n"
"Age : %d\nPhone numbers:\n",
' ', count, man -> name, man -> age
);
number_print(ma n -> phone_numbers);
node = node -> next;
}
}

void number_print(li st_type *node)
{
while (node != NULL) {
printf(" %s\n", (char *)node -> data);
node = node -> next;
}
}

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

void list_free(list_ type *node, void (*free_data)(vo id *))
{
list_type *next_node;

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

void person_free(voi d *data)
{
list_free(((str uct person *)data) -> phone_numbers, free);
free(data);
}

/* END listolists.c */

--
pete
Nov 14 '05 #4
pete wrote:

head = man_init(head, seed);
man_print(head) ;
/* The above doesn't really do what I want */
if (man_init(head, seed) != NULL) {
man_print(head) ;
} /* This is better */
list_free(head, person_free);
puts("\n/* END listolists.c output */");
return 0;
}


--
pete
Nov 14 '05 #5
thanks a lot for all. your answer really helps me.

Nov 14 '05 #6

"jwvai316" <jw******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I don't really know how to say it so I just say it a nested linklist.

How do you make LinkLists inside LinkList?
Can anyone help me for this?
I think an example program will help me a lot.

thank you.


If you make a mistake in writing all the linkages, an easy way to debug it
is make it all doubly linked and have your code traversed it from the last
"tail".
Nov 14 '05 #7

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

Similar topics

2
3011
by: jova | last post by:
I'm stuck I have to convert an array to a linklist the array is initialized as public Organization{ Crew members = new Member etc....
7
3140
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. #include<stdio.h> #include<stdlib.h> struct node { int data;
2
5252
by: prakashgkhaire | last post by:
i have two structure where first structure consists val and structure pointer(linklist), 2nd structure consists, val, a varialbe of first structure, pointer of structure. Now i found to pass the variable of 2nd structure to function as variable of 1st structure struct first { int val; struct first *next;
0
1654
by: prashant0903 | last post by:
how i 'll made the linklist program in java both included insertion & deletion in program
5
2046
by: Asembereng | last post by:
Hi i want to do insertion sort, merge sort and quick sort on my linkList of ints.. do anyone have an idea how to do it?? these are the codes. And i also need to define a lifo fifo class.. class UTGLLElement { public int myData; // data item
2
2095
by: zubia | last post by:
hi how 2 deal with the rptr n lptr in doubly linklist
1
2388
by: smoothkriminal1 | last post by:
Write a Code that will pick timetable of 40 students from file using linklist n than find a slot where all the students dont have any class. file can be of any format Student can maximum take 6 lectures a day consist of 90mins each. I have not tried this problem yet becuase i dont know how to get data from file using linklist. Dnt give me a code for getting data from file but atleast give me idea how to get data from file so that i become...
2
1924
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain; down here am posting my code; please somebody who can point it out; let me know fast; whats wrong in it... //Delete a number next to the given number void afterdelete() { struct linklist*temp=NULL; int no=0;
2
2106
by: dynamo | last post by:
this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function but i see nothing wrong can you help.Thanx #include <iostream> #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <fstream> using namespace std;
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3967
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 we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.