Hello I have got this linked list in java that is done iteratively , it uses a linked list node called LLNode , since it is done iteratively I just have to know how you can implement it recursively , can anybody show me by using the codes I have provided how to convert the iterative algorithms of " AddItem " , " InsertItem " and " DeleteItem " to recursive ones ?
Thank you
// -------------------------------------------
package ads2;
/*
* This data structure for a collection will store String objects in the order
* requested through the methods this class exposes to the "outside world".
*/
public class LinkedList
{
private LLNode firstNode;
private int count;
// Default constructor
public LinkedList()
{
firstNode = new LLNode(null);
count = 1;
}
/* Return the number of items contained within this data structure */
public int GetNoOfItems()
{
return count;
}
/* Returns the String value held at index (base zero) or null if the index
* is out of bounds */
public String GetItemByIndex(int index)
{
LLNode tmp = firstNode;
for(int i = 0; i < index; i++)
{
tmp = tmp.getNext();
}
return tmp.getData();
}
/* Adds value to the end of the data structure */
public void AddItem(String value, LLNode nodex)
{
if ( nodex.getNext() == null )
{
LLNode newNode = new LLNode(value);
nodex.setNext(newNode);
}
else
{
AddItem(value, nodex.getNext());
}
}
/* Inerts value into the data structure at index (base zero) or at the end
* if there are less items in the data structure than index */
public void InsertItem(int index, String value)
{
LLNode newNode = new LLNode(value);
LLNode tmp = firstNode;
for(int i = 0; i < index; i++)
{
tmp = tmp.getNext();
}
LLNode cpy = tmp.getNext();
tmp.setNext(newNode);
newNode.setNext(cpy);
count++;
}
/* Removes the item at index from the data structure - if index is out of
* bounds then the data structure remains unchanged */
public void DeleteItem(int index)
{
LLNode tmp = firstNode;
for(int i = 0; i < index; i++)
{
tmp = tmp.getNext();
}
LLNode toDelete = tmp.getNext();
LLNode nextNode = toDelete.getNext();
tmp.setNext(nextNode);
count--;
}
/* if you want extra internal information about the state of your linked list when
* tested by the unit tests, update the following toString method to dump
* any information you are interested in */
public String toString()
{
return super.toString();
}
}
// ------------------------------------------
// LLNode class :
package ads2;
public class LLNode
{
// you might find it useful to create some constructors here to make your life easier
/* constructors for this class would look like
*
* public ADS2LLNode()
* {
*
* }
*
* and maybe with some parameters (c.f. methods)
*/
// This is the data value that the node holds
private String data;
// This is the next node within the linked list
private LLNode next;
public LLNode(String value)
{
data = value ;
next = null;
}
public LLNode(String value, LLNode nextNode)
{
data = value;
next = nextNode;
}
public String getData()
{
return data;
}
public LLNode getNext()
{
return next;
}
public void setNext(LLNode newNode)
{
next = newNode;
}
}