C++ recursion

h994422's Avatar, Join Date: Sep 2010
Go4Expert Member
Hi.I have to convert a function to recursive type.It insert a new integer to the array based linked list.My recursion does not work properly. Need help. Thanks.

Function:
Code:
void ListRecursive::insert ( int index , ListItemType newItem , bool& success )
{
if((index>=1) && (index<=size+1) && (size<MAX_SIZE))
	{
		for(int pos=size ; pos>=index ; --pos)
			items[translate(pos+1)]=items[translate(pos)];
		//insert new item
		items[translate(index)]=newItem;
		++size;	//increase the size of the list by one
	}
}

Recursive Type:
void ListRecursive::insert ( int index , ListItemType newItem , bool& success )
{
        int pos=size;
	if(pos==index)
		items[index]=newItem;
	else
		return insert(index-1, newItem , success);
}

Last edited by shabbir; 16Jan2011 at 12:22.. Reason: Code blocks
h994422's Avatar, Join Date: Sep 2010
Go4Expert Member
a newer version.Works on an empty list. But still doesn't work on non-empty lists.

Code:
void ListRecursive::insert ( int index , ListItemType newItem , bool& success ) 
	
{
	if(size==index-1)
	{	
		items[size]=newItem;
		++size;
	}
	else
		return insert(index-1, newItem , success);
}

Last edited by shabbir; 16Jan2011 at 12:23.. Reason: Code blocks
h994422's Avatar, Join Date: Sep 2010
Go4Expert Member
finally..!
is there any problem with this code? i think i made it..!
Code:
void ListRecursive::insert ( int index , ListItemType newItem , bool& success ) 
	
{
	if(size==index-1)
		
		items[size++]=newItem;
	}
	else
	{
		items[size]=items[size-1];
		--size;
		return insert(index, newItem , success);
	}
}

Last edited by shabbir; 16Jan2011 at 12:23.. Reason: Code blocks