将迭代链接列表转换为递归 [英] convert iterative link list to recursive

查看:79
本文介绍了将迭代链接列表转换为递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我在java中有这个迭代完成的链表,它使用一个名为LLNode的链表节点,因为它是迭代完成的我只需要知道如何递归地实现它,任何人都可以通过使用我提供了如何将AddItem,InsertItem和DeleteItem的迭代算法转换为递归算法的代码?



谢谢







// ---------------------- ---------------------



包广告2;



/ *

*这个集合的数据结构将按照此类暴露给外部的方法的顺序存储String对象

*世界。

* /



公共类LinkedList

{



私有LLNode firstNode;

私有int计数;





//默认构造函数

public LinkedList()
{

firstNode = new LLNode(null);

count = 1;

}



/ *返回此数据结构中包含的项目数* /

public int GetNoOfItems()

{

返回计数;

}



/ *返回索引处保存的String值(基数为零)或null index

*超出范围* /

public String GetItemByIndex(int index)

{

LLNode tmp = firstNode;

for(int i = 0;我<指数; i ++)

{

tmp = tmp.getNext();

}

返回tmp.getData() ;

}



/ *将值添加到数据结构的末尾* /

public void AddItem (字符串值,LLNode nodex)

{

if(nodex.getNext()== null)

{

LLNode newNode = new LLNode(value);

nodex.setNext(newNode);

}

else

{

AddItem(value,nodex.getNext());

}





}



/ * Inerts在索引(基数为零)或最后的数据结构中的值

*如果数据结构中的项目少于索引* /

public void InsertItem(int index,String value)

{

LLNode newNode =新的LLNode(值);

LLNode tmp = firstNode;

for(int i = 0;我<指数; i ++)

{

tmp = tmp.getNext();

}

LLNode cpy = tmp.getNext ();

tmp.setNext(newNode);

newNode.setNext(cpy);

count ++;

}



/ *从数据结构中删除索引处的项目 - 如果索引超出

* bounds那么数据结构仍然存在不变* /

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--;

}



/ *如果您需要有关链接列表状态的额外内部信息/>
*由单元测试测试,更新f转发toString方法转储

*您感兴趣的任何信息* /

public String toString()

{

返回super.toString();

}

}









// ------------------------------- -----------



// LLNode类:





包广告2;



公共类LLNode

{

//你可能会发现在这里创建一些构造函数以使你的生活更轻松很有用

/ *这个类的构造函数看起来像

*

* public ADS2LLNode( )

* {

*

*}

*

*也许有一些参数(参见方法)

* /



//这是节点持有的数据值

私有字符串数据;

//这是链接列表中的下一个节点

私有LLNode接下来;



公共LLNode(字符串值)

{

data = value;

next = null;

}



公共LLNode(字符串值,LLNode nextNode)

{

data = value;

next = nextNode;

}



public String getData()

{

返回数据;

}



公共LLNode getNext()

{

返回下一个;

}



public void setNext(LLNode newNode)

{

next = newNode;

}

}

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;
}
}

推荐答案

以下是一些可能对您有帮助的链接创建递归函数:



Recursion Java [ ^ ]

计算机科学概论 - Java [ ^ ]
Here are some links that might help you to create recursive functions:

Recursion Java[^]
Introduction to Computer Science - Java[^]


这篇关于将迭代链接列表转换为递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆