gpt4 book ai didi

java - 在单向链表中添加双向链表节点

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:41 26 4
gpt4 key购买 nike

给定单链表:1 -> 2 -> 3 -> 4 -> 5 -> null
修改中间元素为双向链表节点
这里的中间元素是 3
3 -> next 应该指向 4
3 -> prev 应该指向 1

任何人都可以建议如何做到这一点?面试官给了我提示使用界面。但我无法弄清楚如何。
我必须遍历这个链表并打印所有节点,当它到达中间时,打印 next 和 prev 指向的位置,然后打印到列表的末尾。
预期输出:1, 2, Middle: 3, Prev: 1, Next: 4, 5

我在添加中间节点时遇到问题。

最佳答案

所以,这个“有效”,但如果希望在面试中回答这个问题,那就太麻烦了。

链表

public class LinkedList {

public interface Linkable<V, L extends Linkable> {
V getValue();
L getNext();
void setNext(L link);
}

public static class Node implements Linkable<Integer, Linkable> {
int value;
Linkable next;

Node(int value) {
this.value = value;
}

@Override
public Integer getValue() {
return value;
}

@Override
public Linkable getNext() {
return next;
}

@Override
public void setNext(Linkable link) {
this.next = link;
}
}

private Linkable head;

public boolean isEmpty() {
return this.head == null;
}

public Linkable getHead() {
return head;
}

public void add(int v) {
Node next = new Node(v);
if (isEmpty()) {
this.head = next;
} else {
Linkable tmp = this.head;
while (tmp.getNext() != null) {
tmp = tmp.getNext();
}
tmp.setNext(next);
}
}
}

界面

interface DoublyLinkable<V, L extends LinkedList.Linkable> extends LinkedList.Linkable<V,L> {
LinkedList.Linkable getPrev();
void setPrev(LinkedList.Linkable prev);
}

双节点

public class DoubleNode extends LinkedList.Node implements DoublyLinkable<Integer, LinkedList.Linkable> {
LinkedList.Linkable prev;

public DoubleNode(int value) {
super(value);
}

@Override
public LinkedList.Linkable getPrev() {
return prev;
}

@Override
public void setPrev(LinkedList.Linkable prev) {
this.prev = prev;
}
}

司机

输出

1, 2, Middle: 3, Prev: 1, Next: 4, 5

public class Driver {

public static LinkedList getList() {
LinkedList list = new LinkedList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}

public static void main(String[] args) {

LinkedList list = getList();

LinkedList.Linkable head = list.getHead();
LinkedList.Linkable beforeMiddle = null;
LinkedList.Linkable middle = list.getHead();
LinkedList.Linkable end = list.getHead();

if (head != null) {
// find the middle of the list
while (true) {
if (end.getNext() == null || end.getNext().getNext() == null) break;

beforeMiddle = middle;
middle = middle.getNext();

end = end.getNext().getNext();
}

// Replace middle by reassigning the pointer to it
if (beforeMiddle != null) {

DoubleNode n = new DoubleNode((int) middle.getValue()); // same value
n.setPrev(list.getHead()); // point back to the front
n.setNext(middle.getNext()); // point forward to original value

beforeMiddle.setNext((DoublyLinkable) n);
middle = beforeMiddle.getNext();
}

// Build the "expected" output
StringBuilder sb = new StringBuilder();
final String DELIMITER = ", ";
head = list.getHead();
boolean atMiddle = false;
if (head != null) {
do {
if (head instanceof DoublyLinkable) {
atMiddle = true;
String out = String.format("Middle: %d, Prev: %d, ", (int) head.getValue(), (int) ((DoublyLinkable) head).getPrev().getValue());
sb.append(out);
} else {
if (atMiddle) {
sb.append("Next: ");
atMiddle = false;
}
sb.append(head.getValue()).append(DELIMITER);
}

head = head.getNext();
} while (head != null);
}
sb.setLength(sb.length() - DELIMITER.length());

System.out.println(sb.toString());
}

}
}

关于java - 在单向链表中添加双向链表节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com