13 Feb, 2010, Skol wrote in the 1st comment:
Votes: 0
Hey all, I was looking for (and hadn't found) if I could do a linked list of arrays in C.

The usual linked list structure:
———     ———     ———
| a | –+—> | b | –+—> | c | 0 |
——— ——— ———


Currently I'm thinking for my clan roster code, something like this:
—————–    —————–    —————–
| a[0/1/2] | –+—> | b[0/1/2] | –+—> | c[0/1/2] | 0 |
—————– —————– —————–


//pseudo code structure
typedef struct roster_list
{
char *name;
int clan;
int clan_level;
int ch_level;
struct roster_list *next;
} ROSTER_LIST;

extern ROSTER_LIST *dragonarmy_list;
extern ROSTER_LIST *mercenary_list;
// etc etc.

Each node being this struct:
struct  clan_roster_data
{
char ch_name[32];
int clan;
int clan_level;
int ch_level;
};


This is my thoughts so far:
CLAN_ROSTER_LIST *
add_name_to_roster(char *name, int clan, int clan_level, int ch_level, ROSTER_LIST *list)
{
ROSTER_LIST *search;
ROSTER_LIST *head = list;

search = head;

/* Check to see if the name is already listed. */
while (NULL != search)
{
if (!str_cmp(search->name, name))
{
/* blah, if clanrank has changed, unlink that node, make new, then sort */
return(head);
}
search = search->next;
}

/* Alloc memory and add the name. */
search = (ROSTER_LIST *)malloc(sizeof(ROSTER_LIST));
search->name = str_dup(name);
search->clan = clan;
search->clan_level = clan_level;
search->ch_level = ch_level;
search->next = head;
return(search);
}


On more research, it looks like I'm trying to create a Chunk list,
but I couldn't find squat on them as far as a structure etc.
13 Feb, 2010, Runter wrote in the 2nd comment:
Votes: 0
Quote
Hey all, I was looking for (and hadn't found) if I could do a linked list of arrays in C.

Yes.
13 Feb, 2010, David Haley wrote in the 3rd comment:
Votes: 0
I'm not sure what your question is here. As Runter said, yes, you can build linked lists of arrays in C. A chunk list is one where, instead of each node in the list storing one element, each node stores many elements (i.e., each node is a "chunk" of list elements). This data structure has various interesting properties that merge ups and downs of both arrays and linked lists. But you're not really building a chunk list here, because in your case the list still contains one element per node (at least judging from your code) – it's just that elements happen to contain further arrays (or something – I don't see any arrays in your code).

Your code doesn't really make clear what you're trying to do either. What are the arrays you speak of?
14 Feb, 2010, Skol wrote in the 4th comment:
Votes: 0
I hear you, mine was basically 'Not sure how to get there from here'.

What I was looking at was each node containing the array with the structure of the char/int/int/int etc(the struct clan_roster_data). I just hadn't seen any examples or much about how a chunk list is set up (other than in the University of Stanford's doc on linked lists, which was what made me go hmm that sounds like what I need).

My thoughts in //blah were say: node 1 instead of just being char *name and a pointer to the next node, would be char *name, int clan_level, int ch_level (like {"joe bob", 3, 34} would be one node, right?).

A regular linked list would be like:
typedef struct roster_list
{
char *name; // info in current node
struct roster_list *next; // next node
} ROSTER_LIST;

So I guess my question is, I want to store not just the single string, but a string, a few ints etc, on that one node. Then access via pointers as above. Would it be 'name' in this case that would be where the information is stored? (IE. search->name[ch_name], search->name[clan], etc, where name had the structure of {"players name", 3, 34}?

Does this make any sense? So rather than just having (string, link to next node), I'm wanting ((string, int, int, int), link to next node). Or some variation of that, but I hadn't been able to find it anywhere (A Book about C, C for Dummy's, TYS C in 24 etc), nor a google search etc. Basically in the 'train car' approach, I want passengers and stats, then the hitch to the next one, rather than just name of the car ;p.
14 Feb, 2010, Runter wrote in the 5th comment:
Votes: 0
Quote
So I guess my question is, I want to store not just the single string, but a string, a few ints etc, on that one node.


This is completely different from your original question. This is pretty much a defacto feature of a linked list.
struct roster_list {
char *name; // info in current node
CLAN_DATA *clan; // more info in current node
int gold; // even more info in current node
struct roster_list *next; // next node
};


ptr->name; // Access name of the current node
ptr->clan; // Access clan of the current node
ptr->gold; // Access gold of current node?


If your problem *really* needs you to pack all your data into a single field, yes, it's possible. But you have to have a really compelling reason to do this over what I just posted.
14 Feb, 2010, David Haley wrote in the 6th comment:
Votes: 0
Oh. OK, so you're not really looking for a chunk list here. You're looking for a linked list of structs. This actually already exists all over the place in SMAUG although it might not be obvious. Look at e.g. the effect structures (AFFECT_DATA) or even the character structures (CHAR_DATA). Those contain next/prev pointers, but a lot of other stuff too.


EDIT: Hmm, what Runter said.
14 Feb, 2010, Skol wrote in the 7th comment:
Votes: 0
David and Runter, awesome, thank you!
I honestly wasn't sure as all I had dealt with was the simple linked lists (really simple, ie string/link).
Thanks! I'll post up what it turns out to be once I get onto it.
And yeah, David exactly on the linked list of structs. I hadn't recognized them for what they were previously, lots of studying now hehe. Thanks guys!
14 Feb, 2010, Runter wrote in the 8th comment:
Votes: 0
You may consider using C++ (or rather just having it enabled) so you can use choice parts of it. For example, you may find the list structure in C++ is easier to deal with than implementing linked lists. At least in the long run.

struct roster_list {
char *name; // info in current node
};


list<struct roster_list*>   global_roster;    // list structure ready to be populated with roster_list objects.

ptr = (struct roster_list*) malloc(sizeof(struct roster_list));
ptr->name; // assign it

r_list.push_front(ptr); // Puts ptr in the front of the list
// Can also use push_back()



r_list.remove(ptr); // Removes ptr from the list.



Since it will come up I should probably iterate through a list.

list<int>  ilist;

ilist.push_front(10);
ilist.push_front(15);
ilist.push_front(20); // list contains 10, 15, 20

// Now we iterator through the list and access each value.
for(list<int>::iterator i = ilist.begin(); i != mylist.end(); i++)
*i; // dereferencing i accesses each element.


In any event, the huge library associated with std::list gives you more functionality than you're likely to find a need for without having to reinvent the wheel each time you need a list structure. (Which is going to be often in many large applications.)
14 Feb, 2010, David Haley wrote in the 9th comment:
Votes: 0
I wouldn't bother with pointers actually.

list<roster_node> global_roster;
roster_node r;
r.name = "bob"; // <– this is assuming that you're using std::string and not char*,
// otherwise use stralloc or whatever
r.level = 27;
global_roster.push_back(r);
0.0/9