Thread: Multilist

  1. #1
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62

    Multilist

    I am looking for a multilist tutorial. Does any one know a good multilist tutorial? Thanks in advance.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    What's a multilist? I can think of a few things that I can call a "multilist", so until you nail down the terminology, finding a tutorial is gonna be hard.

  3. #3
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I don't see where's the problem with that ... it's like going from a 1D array to a 2D array. Every element in your list doesn't represent a value but rather a pointer to yet another list. How you implement that is up to you really.

  5. #5
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Do you have any good example program? I don't have any experience with 2D arrays.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I don't have any experience with 2D arrays.
    Then why are you jumping to a linked version of a 2D array? It's a lot harder to build and work with, and if you don't have experience with 2D arrays, you're going to struggle with the concept of a table as well as the details of linked lists. If you build onto a weak foundation, you'll get a weak structure.

  7. #7
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Could you check my work, please? I wanted to create an empty list, is this right?

    This is the header stuff:
    Code:
    #ifndef _1_H
    #define _1_H
    
    struct car
    {
    	int car_km;
    	char *car_color;
    };
    
    typedef struct node *node_ptr;
    struct node
    {
    	char *car_brand;
    	int numberOfcars;
    	struct car cars[100000];
    	node_ptr next;
    };
    
    /*Function to create an empty list*/
    node_ptr create(void);
    
    #endif
    the function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "car.h"
    
    node_ptr create(void)
    {
    	node_ptr list=(node_ptr)malloc(sizeof(struct node));
    	list->car_brand = 0;
    	list->numberOfcars = 0;
    	struct car;
    	list->next = NULL;
    	return list;
    }
    Code:
    struct car;  ---->  car.c:19: warning: ISO C90 forbids mixed declarations and code
    , what should I do to fix this? Thanks
    Last edited by BoneXXX; 04-20-2007 at 06:38 AM.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It means when you declare variables, you have to declare them all at the top of the code block before you start doing things like function calls, value assignments, etc.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Okay, I have one more question; did that code create me this node?Thanks

    Node
    Car Brand
    Number of cars which are the same brand
    car km,color
    Null
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  10. #10
    Registered User BraneMxm's Avatar
    Join Date
    Apr 2007
    Location
    Croatia
    Posts
    20
    BoneXXX create yourself a print function which takes a pointer to the list and prints out hte nodes you have created or nothing if it isnt created. ;-)

  11. #11
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    I don't know how to get int car_km, char *car_color from struct car?
    Code:
    		printf("&#37;s", current->car_brand);
    		printf("%d", current->numberOfcars);
    		printf("%d", ?????);   /* car km*/
    		printf("%s",  ?????);  /* car color*/
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you doing with your node there? You're making a node (I can only assume to make a linked list) which contains an array of 100000 cars? What on earth for?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Just for an exercise. it can be 10.

    Code:
    #ifndef _1_H
    #define _1_H
    
    struct car
    {
    	int car_km;
    	char *car_color;
    };
    
    typedef struct node *node_ptr;
    struct node
    {
    	char *car_brand;
    	int numberOfcars;
    	struct car cars[10];
    	node_ptr next;
    };
    
    /*Function to create an empty list*/
    node_ptr create(void);
    
    #endif
    the function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "car.h"
    
    node_ptr create(void)
    {
    	struct car;
            node_ptr list=(node_ptr)malloc(sizeof(struct node));
    	list->car_brand = 0;
    	list->numberOfcars = 0;
    	list->next = NULL;
    	return list;
    }
    
    /*Function to print list*/
    void print(node_ptr list)
    {
    	node_ptr current = list->next;
    	printf ("The list is:");
    	while (current != NULL);
    	{
    		printf("&#37;s", current->car_brand);
    		printf("%d", current->numberOfcars);
    		printf("%d", ?????);   /* car km*/
    		printf("%s",  ?????);  /* car color*/
    	}
    	printf("\n");
    }
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to have missed the point. Why is that information its own structure?

    Anyway, what part don't you know how to access? Members of an array?
    Code:
    x = array[ y ];
    Members of a structure?
    Code:
    x = foo.x;
    An array that's part of a structure?
    Code:
    x = foo.bar[ y ];
    Members of a structure that are part of another structure?
    Code:
    x = foo.bar.baz;
    Members of an array of structures that are part of a structure?
    Code:
    x = foo.bar[ y ].baz;
    Members of an array that are in a structure, of which you have an array, inside another structure?
    Code:
    x = foo.bar[ y ].baz[ z ];
    See how that works? You can go on and on.


    Quzah.
    Last edited by quzah; 04-20-2007 at 08:34 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed