Thread: How do I utilize an array of linked lists?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    How do I utilize an array of linked lists?

    The concept is easy enough to get. I just can't find the paper I wrote down how to do it on...

    So my question is just how to make an element of an array of linked lists refer to an element of the list.

    So if I have:

    Code:
    struct list[20]
    {
    int num;
    struct list *next;
    }
    Is that the proper way to start one off?

    And how do I refer to an element inside? Something like head[2]->next, or head[2].next?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    struct list {
        int num;
        struct list *next;
    } arrayOfLists[50];
    The rest is OK.

    But use a typedef to separate the type declaration from the instance of a variable of that type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Thanks for the help.

    But I still don't know how to actually call an element of a particular list. Is it something like:

    Code:
    test[3]->num=5;
    
    //or
    
    test[3].num=5;
    All I need is 1 line of code to see how this is done

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try it.
    One compiles, the other doesn't.

    But FWIW, an array of linked lists would be more like
    struct list *arrayOfLists[20];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would not use an array to deal with a linked list -- there is no point. Part of the idea of a linked list is that you do not use any particular fixed variable (or array) to refer to "an element" (so call it a node, not an element). You use temporary pointers, and by assigning memory with malloc. Normally, if you did this and then threw the pointer away or reassigned it, this memory would be considered "leaked". But with a linked list, you can find a chunk of memory by using the pointers included in the node structure. So the answer to your last question is you always use -> because *node is always a pointer. Look:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _node {
    	int X;
    	struct _node *next;
    } node;
    
    void addnode (node *head, int value) {
    	node *new=malloc(sizeof(node)), *ptr=head;
    	new->X=value; new->next=NULL;
    	while (ptr->next) ptr=ptr->next;  /* find the "tail" */
    	ptr->next=new;
    }
    
    void showlist (node *head) {
    	node *ptr=head;
    	while (ptr->next) {
    		printf ("%d\n",ptr->X);
    		ptr=ptr->next;
    	}
    	printf ("%d\n", ptr->X);
    }
    	
    
    int main() {
    	node *head=malloc(sizeof(node));
    	int i;
    	head->X=100;
    	for (i=0; i<5; i++) addnode(head,(i+2)*100);
    	showlist(head);	
    	return 0;
    }
    100
    200
    300
    400
    500
    600


    That's a singly linked list because there is no "previous" pointer. They are great way to improve your understanding of pointers and memory assignment. Notice how head is used; this is the essence of the linked list method.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Thanks a lot both of you, especially MK. That's a nice, simple example of a linked list.

    We have to use them for my programming class, so I don't have much of a choice. It wouldn't surprise me if it was just to improve understanding of pointers and memory assignment.

    By the way, head[2]->next; and head[2].next aren't working. I tried them before I posted.
    Last edited by sharrakor; 02-14-2009 at 10:25 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sharrakor View Post
    By the way, head[2]->next; and head[2].next aren't working. I tried them before I posted.
    In what context?

    Again, if you are using an array to deal with a linked list, I would say you are barking up the wrong tree. There are important and significant differences between between an array and a linked list. They are not the same thing.

    So you should not have a head[2]. Or a head[0], etc. The "head" is the first node in the list, NOT the first element of an array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Got it. Thanks for the time guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Linked Lists - Hash table
    By FortinsM3 in forum C Programming
    Replies: 12
    Last Post: 02-25-2009, 10:20 PM
  2. Linked List and Array of Pointers
    By Nish in forum C Programming
    Replies: 4
    Last Post: 03-04-2005, 04:28 PM
  3. Replies: 5
    Last Post: 11-20-2001, 12:48 PM
  4. Linked lists
    By sballew in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 08:52 PM
  5. I need some help on my linked lists app
    By valar_king in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:36 PM