Sunday, July 12, 2009

How to make a funcition that returns a pointer to the head of a list in C?

Hi i need to write a funcition called install, its takes as arguments


1- a vector


2-a number


3-a list called next_level





so heres where it get kinda weird:





next_level is a list comprising of elements that are { vectors, with


pointers}, defined by the following struct





typedef struct wnode {


int weight[maxrank+1];


struct wnode *next;





} WEIGHTPTR,*WEIGHTPTR;





Now install is a funcition that returns a pointer to the frist vector


in the list, Can anyone give the code to define this funcition.?





Thanks:

How to make a funcition that returns a pointer to the head of a list in C?
I am not sure how you are making the list, I understand about the elements in the list.


You could do a number of things:


Firstly, not to make the struct definition different, you could create a list structure. This struct could have a typedef like:


typedef struct list {


struct wnode *first;


}


Then when passing the list to the function, the function can return the 'first' pointer.





Second way, create a double ended list, but this changes the wnode struct.


typedef struct wnode {


int weight[maxrank+1];


struct wnode *next;


struct wnode *prev;


} WEIGHTPTR, *WIEGHTPTR


You could then transverse backwards through the list to get to the first element. The first element should have a null pointer as the 'prev' element, and the last element should have a null pointer as the 'next' element.





The first seems easier, but will require you making a list, and filling it appropriately.


The second may change what you need to do, and also may have no relevance to what you are trying to do. An element of the list is not being passed into the function, only the list is.


No comments:

Post a Comment