c# - Recursive insert-method for a linked list -


i'm learning c# , i've made recursive insert-method linked list:

public static int recursiveinsert(ref int value, ref mylinkedlist list) {     if (list == null)         return new mylinkedlist(value, null);     else {         list.next = recursiveinsert(ref int value, ref list.next);         return list;     } } 

how can modify method make recursive call this:

recursiveinsert(value, ref list.next) 

instead of:

list.next = recursiveinsert(ref int value, ref list.next); 

since never mutating parameters passing reference, can not pass them reference @ all. need recognize mylinkedlist being reference type (it should absolutely reference type) means you're not passing value of object itself, you're passing reference it, can mutate reference without passing reference reference.) remove uses of ref (and fix return type correct) , you're done:

public static mylinkedlist recursiveinsert(int value, mylinkedlist list) {     if (list == null)         return new mylinkedlist(value, null);     else     {         list.next = recursiveinsert(value, list.next);         return list;     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -