Click here to Skip to main content
15,886,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}
}
Posted
Comments
George Jonsson 17-Dec-14 23:41pm    
You should use the 'Improve question' widget and format your code properly.
This is too much to read.

1 solution

Here are some links that might help you to create recursive functions:

Recursion Java[^]
Introduction to Computer Science - Java[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900