简体   繁体   中英

multi head linked list

I am working with linked lists in c but my program needs actually a big number of linked lists at the same time and so when I want to add a new node I must determine to which list should I add, I have a function which return the head of the desired list to add but I am a little bit confused how should I write the "add_node" function because the head node is not the same every time and I don't want to use the "switch case" because it would be very long... Thanks in advance

void add_it(int *array)
{
    head=which_head(array);
    curr = malloc(sizeof (node));
    memcpy(curr->nconn, array, sizeof (curr->nconn));
    curr->next = ?????????;
    ???????=curr
}
struct Node
{
    Node *next;
    int   myData[80];
};

Node **which_head(some params);

void Add(some data)
{
    Node **head_location = whitch_head(...);
    Node *new_element = (Node*)malloc(...);

    new_element->next = *head_location;
    *head_location = new_element;
}

The trick is that your which_head() function should return location of the head, not the pointer to the first element of the list if any.

You can keep the linked list heads in an array or again in another linked list. I will tell you to keep the linked list functions general and should not have code to handle multiple lists. When you need to add some node to a particular linked list, you can fetch it using a function from the array/linked list and then use the returned head pointer to the general linked list functions. Thus you can keep the extra code for handling multi-head issue outside the linked list routines.

If I am getting your question correctly, You want some linklists with having different headers for each. Now you want to access all linklists in a same manner.

My solution is Why did not you try Hash Table ?

Try Hash Table and you will get solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM