Java:单链表,虚拟节点,插入 public类A扩展抽象列表{ 私有SLNode头=新SLNode private int length=0;//列表的长度 //我将跳过SLNode的课程 //Head的元素和后续元素在类SLNode中初始化为null public void add(int index,E element)//在列表中插入一个元素 { //如果索引小于0或大于长度 如果((索引长度)) 抛出新的IndexOutOfBoundsException(); 如果(索引==0) { SLNode newnode=新SLNode(元素,null);//创建新节点 newnode.setSuccessor(Head.getSuccessor()); Head.setSuccessor(newnode); 长度++; } }
这是在列表前面添加元素的正确方法吗?(使用伪头节点,但不使用尾节点)Java:单链表,虚拟节点,插入 public类A扩展抽象列表{ 私有SLNode头=新SLNode private int length=0;//列表的长度 //我将跳过SLNode的课程 //Head的元素和后续元素在类SLNode中初始化为null public void add(int index,E element)//在列表中插入一个元素 { //如果索引小于0或大于长度 如果((索引长度)) 抛出新的IndexOutOfBoundsException(); 如果(索引==0) { SLNode newnode=新SLNode(元素,null);//创建新节点 newnode.setSuccessor(Head.getSuccessor()); Head.setSuccessor(newnode); 长度++; } },java,singly-linked-list,Java,Singly Linked List,这是在列表前面添加元素的正确方法吗?(使用伪头节点,但不使用尾节点) 问题2.列表是空的还是非空的都是一样的吗?这样做并不坏,尽管使用“dummy”节点来表示“head”有点不寻常 但它的优点是,您可以将“Head”替换为“current”,将其初始化为“Head”,然后在列表索引节点上“爬行”并进行插入,并且不需要特殊情况零情况 public class A<E> extend AbstractList<E>{ private SLNode<E> Head
问题2.列表是空的还是非空的都是一样的吗?这样做并不坏,尽管使用“dummy”节点来表示“head”有点不寻常 但它的优点是,您可以将“Head”替换为“current”,将其初始化为“Head”,然后在列表
索引节点上“爬行”并进行插入,并且不需要特殊情况零情况
public class A<E> extend AbstractList<E>{
private SLNode<E> Head = new SLNode<E>
private int length = 0; // length of the list
// I will skip the class of SLNode<E>
// Head's element and successor is initialized as null in the class SLNode<E>
public void add(int index, E element) // insert an element in the list
{
// if index is less than 0 or greater than the length
if( (index < 0) || (index > length ) )
throw new IndexOutOfBoundsException();
if(index ==0)
{
SLNode<E> newnode = new SLNode<E>(element, null); // make new node
newnode.setSuccessor(Head.getSuccessor());
Head.setSuccessor( newnode);
length++;
}
}
public void add(int-index,E-element)//在列表中插入一个元素
{
//如果索引小于0或大于长度
如果((索引<0)| |(索引>长度)){
抛出新的IndexOutOfBoundsException();
}
//查找插入点
SLNode电流=磁头;
对于(int i=0;i
(但请注意,标准命名约定是为类名保留首字母大写的名称。)假设这是您的头插入方法,我不明白您为什么需要传入索引。理想情况下,我希望只有一个方法用于插入。但由于您特别要求头插入
public void add(int index, E element) // insert an element in the list
{
// if index is less than 0 or greater than the length
if( (index < 0) || (index > length ) ) {
throw new IndexOutOfBoundsException();
}
// Find insertion point
SLNode<E> current = Head;
for (int i = 0; i < index; i++) {
current = current.getSuccessor();
}
// Create & insert new node
SLNode<E> newnode = new SLNode<E>(element, null);
newnode.setSuccessor(current.getSuccessor());
current.setSuccessor( newnode);
length++;
}
这是在列表前面添加元素的正确方法吗?
(使用虚拟头节点,但不使用尾部)
编辑:在重新阅读你的问题,并尝试你的代码,我会说是的
问题2.列表是空的还是非空的是否相同
编辑:是的,它似乎在空列表上工作
public void addToHead(E element) // insert an element at head
{
if(length==0)
throw new Exception("head does not exist");
element.setSuccessor(head);
head = element;
length++;
}
使用此toString:
[]
[first add]
[second add,first add]
[third add,second add,first add]
公共字符串toString()
{
字符串输出=“[”;
SLNode temp=Head.getsuccessiver();
while(temp!=null){
if(output.length()==1){
输出=输出+温度toString();
}
否则{
输出=输出+”,“+温度toString();
}
temp=temp.getsuccession();
}
输出=输出+“]”;
返回输出;
}
我认为在列表的前面插入并不需要特殊的情况。在列表的前面插入与在列表中的其他位置插入没有什么不同
public String toString()
{
String output = "[";
SLNode<E> temp = Head.getSuccessor();
while ( temp != null ) {
if ( output.length() == 1 ) {
output = output + temp.toString();
}
else {
output = output + "," + temp.toString();
}
temp = temp.getSuccessor();
}
output = output + "]";
return output;
}
public void add(int索引,E元素)
{
//如果索引小于0或大于长度
如果((索引<0)| |(索引>长度))
抛出新的IndexOutOfBoundsException();
SLNode previousNode=头部;
对于(int i=0;i
基本上,这只是遍历列表以找到要插入的正确节点-如果索引为0,它将立即在头部停止。一旦有了要插入的节点,您将执行以下三个步骤:
创建新节点并将其后续节点设置为上一个节点的后续节点
将上一个节点的后续节点设置为新节点
在列表长度中添加一个
我对此进行了一些小测试,它似乎运行得很好
注意,这种方法几乎假设head永远不会被视为列表中的一个元素。它实际上只是一个开始列表的地方。我的意思是head.getElement()将始终返回null-它不是列表的一部分。我不确定这是否是您想要的实现,但当您说以head元素开始列表时,似乎最有意义,即使列表应该是空的。这是您的常规插入方法还是在head插入方法?是的..我应该添加元素位于前面,使用伪头节点,但没有尾部指针..小提示,与答案无关:不要以大写字母开头变量。类以大写字母开头。GetSuccession()看起来您正在访问Head类中的静态方法。如果它只是在Head处插入,为什么要将索引传递给该方法?因为您知道将其插入到何处。Seconly在Head处插入将不需要更新尾部指针。我还有If(index>0),但只想检查我的(index==0)是否正确谢谢!(你不必先创建一个新的节点来添加???)你传递到方法中的元素本质上不是你想要添加的节点吗?我想是的。谢谢!(但是Head(对于伪头节点)不是总是必须指向空的节点吗??例如Head->null->element&succession???@hibc我相信Head代表“列表的头”.Head将是列表中的第一个值(如果列表中有项目),然后将指向下一个项目(如果存在),该项目将指向下一个项目(如果存在)Head->Node 2->Node 3->Node 4->null列表中的最后一个节点应该引用null。在这种情况下,只有当列表中有0或1个项目时,Head才应该指向null。@hibc我重新阅读了问题,现在我明白了。您正在做一个非传统的链接列表。我尝试了代码,它似乎工作正常。
public String toString()
{
String output = "[";
SLNode<E> temp = Head.getSuccessor();
while ( temp != null ) {
if ( output.length() == 1 ) {
output = output + temp.toString();
}
else {
output = output + "," + temp.toString();
}
temp = temp.getSuccessor();
}
output = output + "]";
return output;
}
public void add(int index, E element)
{
// if index is less than 0 or greater than the length
if ((index < 0) || (index > length))
throw new IndexOutOfBoundsException();
SLNode<E> previousNode = head;
for ( int i = 0; i < index; i++ ) {
previousNode = previousNode.getSuccessor();
}
SLNode<E> newNode = new SLNode<E>(element, previousNode.getSuccessor());
previousNode.setSuccessor(newNode);
length++;
}