Hi guys,

By any chance do any of you guys know any way to implement an array of linked list without using vector, since i need to implement an array of linked list using c and unfortunately vector library is unavailable.

This is what i did:

Struct MyList{
  int size;
  int stat;
  struct Mylist *next;
};
typedef stryct Mylist node;

void create_node(node *current,  int size, int stat){
   current -> size = size;
   current -> stat = stat;
   current -> next = NULL;
}

int main(){
  node *ilist =NULL;
  //i am not sure how to implement an array of linked list without vector
  node *list[10];
  list[0] = create_node(ilist, 32, 10); 
}

Thanks Heaps!

Amy

//i am not sure how to implement an array of linked list without vector

And that's why I always say: start with C first.
If you're making linked list, you don't use [].

Inside your main() you just hold head pointer which points at first node.

Your create_node code has to be something like:

int create_node(node* head, int a_size, int a_stat){
    //now we create new node using malloc function, similar to new in c++
    node *temp = malloc(sizeof(node));
    
    //on error (node is not created)
    if (temp == NULL) return -1;

    //temp is now pointing to that newly created node, let's fill it
    temp->size = a_size;
    temp->stat = a_stat;
    temp->next = NULL; //new node points at nothing
    //and now tricky part, as we want our new node to point to previous node
    node* prev = head; //prev is pointing to first now
    if (prev == NULL) //this is actually first node
        head = temp; //so head should point to it
    else{ //but if it's not
        while (prev->next != NULL) //loops until last node in list
            prev = prev->next //advance to next node
        //while has stoped, now we link our newly created node to back of list
        prev->next = temp;
    }
    return 0;
}

I think I've been more than helpful :)

Oh yes, and, code is not compiled, so there possibly are some errors

This article has been dead for over six months. Start a new discussion instead.