在 LinkedList 中实现 AddAtIndex 方法
Implementing an AddAtIndex method in a LinkedList
目前,我正在努力实施 AddAtIndex 方法,并且在大多数情况下它似乎工作正常。但是,我的方法没有通过我的 JUnit 测试,我似乎不明白为什么。因此,我选择展示到目前为止我完成的代码:
**
* Add an element to the list at the specified index
* @param The index where the element should be added
* @param element The element to add
*/
public void add(int index, E element ) //Method should be O(1) time.
{
// TODO: Implement this method
if (index < 0) {
System.out.println("Can't add an element at a negative index.");
}
int i = 0;
LLNode<E> currentNode = head.next;
while ( i < size ) {
if ( i == index ) {
LLNode<E> newNode = new LLNode<E>(element);
LLNode<E> tempNode = new LLNode<E>(currentNode.data);
currentNode.next = tempNode;
currentNode.data = newNode.data;
newNode.prev = currentNode.prev;
newNode.next = tempNode;
tempNode.prev = newNode;
size++;
}
currentNode = currentNode.next;
i++;
}
}
我的代码背后的思考过程是该方法创建一个新节点,然后它替换链表指定索引处的数据。然而,它正在替换的节点上的数据存储在一个临时节点中,该临时节点的位置递增到新节点之后的下一个节点。尽管代码看起来有点草率,但我对我的实现有大约 80% 的信心。我创建了一个驱动程序来演示实现。驱动代码如下:
public class LinkedListDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyLinkedList<String> nameList = new MyLinkedList<String>();
nameList.add("Hamadi");
nameList.add("Ballo");
nameList.add(1, "Salisu");
nameList.add(2, "Galo");
System.out.println(nameList.toString());
System.out.println(nameList.size());
nameList.set(2, "Abdullahi");
System.out.println(nameList.toString());
nameList.remove(1);
System.out.println(nameList.toString());
MyLinkedList<Integer> list1 = new MyLinkedList<Integer>();
list1.add(65);
list1.add(21);
list1.add(42);
System.out.println(list1.toString());
list1.remove(0);
System.out.println(list1.toString());
}
}
驱动程序的输出如下:
List: Hamadi, Salisu, Galo, Ballo,
4
Replacing Galo with Abdullahi
List: Hamadi, Salisu, Abdullahi, Ballo,
Removing Salisu from the list
List: Hamadi, Abdullahi, Ballo,
List: 65, 21, 42,
Removing 65 from the list
List: 21, 42,
但是单元测试失败并出现以下错误:
它在 AssertEquals 方法处失败:
shortList.add(2, "E");
shortList.add(3, "F");
**assertEquals("AddAtIndex: at position 2 ", "E", shortList.get(2)); //fails here**
assertEquals("AddAtIndex: at position 3 ", "F", shortList.get(3));
assertEquals("AddAtIndex: List size is ", 6, shortList.size());
我想知道我做错了什么。虽然我知道我的 AddAtindex 方法有些不对劲,但我确实完全弄清楚了这一点。谢谢!
你不需要那个 tempNode
。只需创建 newNode
并将其正确插入 currentNode
和它的前一个节点之间。
您还应该考虑在列表的开头(没有上一个)或结尾(没有下一个)添加元素的可能性。
我使用头和尾作为哨兵节点。创建了一个要添加到列表中的新节点。
public boolean add(E element) {
// create new element
LLNode<E> variable = new LLNode(element);
variable.next = null;
variable.prev = null;
// if element is null, throw exception
if (element == null) {
// return false;
throw new NullPointerException("Element is null");
} else {
// get the value stored in tail.prev in variable temp.
variable.prev = tail.prev;
variable.next = tail;
// now modify the tail node prev and new node next
tail.prev = variable;
// get prev node next link changed
variable.prev.next = variable;
// update size
if (head.next.next != tail) {
size++;
}
return true;
}
}
目前,我正在努力实施 AddAtIndex 方法,并且在大多数情况下它似乎工作正常。但是,我的方法没有通过我的 JUnit 测试,我似乎不明白为什么。因此,我选择展示到目前为止我完成的代码:
**
* Add an element to the list at the specified index
* @param The index where the element should be added
* @param element The element to add
*/
public void add(int index, E element ) //Method should be O(1) time.
{
// TODO: Implement this method
if (index < 0) {
System.out.println("Can't add an element at a negative index.");
}
int i = 0;
LLNode<E> currentNode = head.next;
while ( i < size ) {
if ( i == index ) {
LLNode<E> newNode = new LLNode<E>(element);
LLNode<E> tempNode = new LLNode<E>(currentNode.data);
currentNode.next = tempNode;
currentNode.data = newNode.data;
newNode.prev = currentNode.prev;
newNode.next = tempNode;
tempNode.prev = newNode;
size++;
}
currentNode = currentNode.next;
i++;
}
}
我的代码背后的思考过程是该方法创建一个新节点,然后它替换链表指定索引处的数据。然而,它正在替换的节点上的数据存储在一个临时节点中,该临时节点的位置递增到新节点之后的下一个节点。尽管代码看起来有点草率,但我对我的实现有大约 80% 的信心。我创建了一个驱动程序来演示实现。驱动代码如下:
public class LinkedListDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyLinkedList<String> nameList = new MyLinkedList<String>();
nameList.add("Hamadi");
nameList.add("Ballo");
nameList.add(1, "Salisu");
nameList.add(2, "Galo");
System.out.println(nameList.toString());
System.out.println(nameList.size());
nameList.set(2, "Abdullahi");
System.out.println(nameList.toString());
nameList.remove(1);
System.out.println(nameList.toString());
MyLinkedList<Integer> list1 = new MyLinkedList<Integer>();
list1.add(65);
list1.add(21);
list1.add(42);
System.out.println(list1.toString());
list1.remove(0);
System.out.println(list1.toString());
}
}
驱动程序的输出如下:
List: Hamadi, Salisu, Galo, Ballo,
4
Replacing Galo with Abdullahi
List: Hamadi, Salisu, Abdullahi, Ballo,
Removing Salisu from the list
List: Hamadi, Abdullahi, Ballo,
List: 65, 21, 42,
Removing 65 from the list
List: 21, 42,
但是单元测试失败并出现以下错误:
它在 AssertEquals 方法处失败:
shortList.add(2, "E");
shortList.add(3, "F");
**assertEquals("AddAtIndex: at position 2 ", "E", shortList.get(2)); //fails here**
assertEquals("AddAtIndex: at position 3 ", "F", shortList.get(3));
assertEquals("AddAtIndex: List size is ", 6, shortList.size());
我想知道我做错了什么。虽然我知道我的 AddAtindex 方法有些不对劲,但我确实完全弄清楚了这一点。谢谢!
你不需要那个 tempNode
。只需创建 newNode
并将其正确插入 currentNode
和它的前一个节点之间。
您还应该考虑在列表的开头(没有上一个)或结尾(没有下一个)添加元素的可能性。
我使用头和尾作为哨兵节点。创建了一个要添加到列表中的新节点。
public boolean add(E element) {
// create new element
LLNode<E> variable = new LLNode(element);
variable.next = null;
variable.prev = null;
// if element is null, throw exception
if (element == null) {
// return false;
throw new NullPointerException("Element is null");
} else {
// get the value stored in tail.prev in variable temp.
variable.prev = tail.prev;
variable.next = tail;
// now modify the tail node prev and new node next
tail.prev = variable;
// get prev node next link changed
variable.prev.next = variable;
// update size
if (head.next.next != tail) {
size++;
}
return true;
}
}