我获得了从头开始创建LinkedList的任务,我已经想过如何编写一个在列表末尾添加节点的方法,但我仍然无法想象如何替换节点。以下是我到目前为止的情况:
public boolean replace(int element, int index) {
Node temp= new Node(pElement);
Node current = getHead();
if (!isEmpty()) {
for (int i = 0; i < index && current.getNext() != null; i++) {
current = current.getNext();
}
temp.setNext(noeudCourant.getNext());
noeudCourant.setNext(temp);
listCount++;
return true;
}
return false;
}
在&#34; 0 1 2 3 4 5 6 7 8 9 10&#34;
上使用 aNode.replace(10,4)将进入
[0] - &GT; [1] - &GT; [2] - &GT; [3] - &GT; [4] - &GT; [ 10 ] - &GT; [5] - &GT; [6] - &GT; [7] - &GT; [8] - &GT; [9] - &GT; [10]
但我想:
[0] - &GT; [1] - &GT; [2] - &GT; [3] - &GT; [ 10 ] - &GT; [5] - &GT; [6] - &GT; [7] - &GT; [8] - &GT; [9] - &GT; [10]
感谢任何帮助。
[edit]我已经有了一个工作方法setData(),但我已禁止使用它。我想要的基本上是这样的:
答案 0 :(得分:0)
如果您真的想要替换节点而不删除它,那么您应该如何编写replace()
函数:
public boolean replace(int element, int index) {
Node head = getHead();
int counter = 0;
while(null != head && counter++ < index - 1) {
head = head.getNext();
}
if(null == head || null == head.getNext()) return false;
Node newNode = new Node(element);
newNode.setNext(head.getNext().getNext());
head.setNext(newNode);
return true;
}
在这里,我假设你的setNext()
课程中有Node
方法来设置链接。
另请注意,这里假设您永远不会替换head
本身,即您永远不会替换索引0
处的元素,否则您将不得不返回新的{{1}来自函数。
答案 1 :(得分:0)
以下是您问题的简单解决方案:
package linkedlist;
class Node {
public Node next = null;
public int element;
public Node(int el) {
element = el;
}
}
class LinkedList {
public Node first = null;
public void add(Node node) {
if (first == null) {
first = node;
} else {
// Traverse to the last
Node cursor = first;
while (cursor.next != null) {
cursor = cursor.next;
}
cursor.next = node;
}
}
public void add(int[] elements) {
int len = elements.length;
for (int i=0;i < len;i++) {
add(new Node(elements[i]));
}
}
public boolean replace(int element, int index) {
Node cursor = first;
Node prev = null;
while (cursor != null && index >= 0) {
index--;
prev = cursor;
cursor = cursor.next;
}
if (index > 0) return false;
if (prev != null)
prev.element = element;
return true;
}
public void displayAll() {
Node cursor = first;
while (cursor != null) {
System.out.print(cursor.element + " ");
cursor = cursor.next;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// Prepare elements
LinkedList linkedList = new LinkedList();
linkedList.add(new int[]{0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
println("Display the initial linked list content:");
linkedList.displayAll();
println("After replace:");
linkedList.replace(10, 4);
linkedList.displayAll();
println("Done");
}
static void println(String msg) {
System.out.println(msg);
}
}
答案 2 :(得分:-1)
尝试以下代码。无论何时需要,我都在评论中写了逻辑。
public boolean replace(int element, int index) {
Node temp= new Node(pElement);
Node current = getHead();
if (!isEmpty()) {
//Run for loop one less than index value.
for (int i = 0; i < index -1 && current.getNext() != null; i++) {
current = current.getNext();
}
// At this point current points to element 3.
// Set next element of node 4 as a next element of new element 10.
temp.setNext(current.getNext().getNext());
// at this point we have two references for element 5 like below
// [0]->[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]->[9]->[10]
// [10]->[5]->[6]->[7]->[8]->[9]->[10]
// Set null to next element of of element 4 to remove reference to
// element 5
current.getNext().setNext(null);
// At this point we have two list as below:
// [0]->[1]->[2]->[3]
// [10]->[5]->[6]->[7]->[8]->[9]->[10]
// Set new element as next of current element (current element is 3)
current.setNext(temp);
// here we have replaced the element 4 with element 10
listCount++;
return true;
}
return false;
}