如何在LinkedList实现中的指定索引处添加节点

时间:2018-01-29 01:37:34

标签: java list implementation

所以我从头开始实现一个LinkedList,一个方法insertAt(int index,T elem)真的让我很头疼。该方法应该在指定的索引处插入一个节点,并相应地移动列表的其余部分。我的实现似乎只是复制和粘贴现有节点。对我做错的任何帮助? (以下用例)

让我知道是否有助于包含界面注释或完整的类:

import tensorflow as tf
import numpy as np

input = [b'testA', b'testB', b'testC']

def my_map_func(input):
  return np.array([input, input, input]), np.array([10, 20, 30])

ds = tf.data.Dataset.from_tensor_slices(input)
ds = ds.map(map_func=lambda input: tf.py_func(
    func=my_map_func, inp=[input], Tout=[tf.string, tf.int64]))
ds = ds.flat_map(lambda mystr, myint: tf.data.Dataset().zip((
  tf.data.Dataset().from_tensor_slices(mystr),
  tf.data.Dataset().from_tensor_slices(myint))
))

element = ds.make_one_shot_iterator().get_next()

with tf.Session() as sess:
  for _ in range(9):
    print(sess.run(element))

节点类:

public class LinkedList<T> implements ListInterface<T> {

private Node<T> first;
private Node<T> last;
private int counter;

public LinkedList() {
}

@Override
public ListInterface<T> insertAt(int index, T elem) {
    if(index > counter) {
        throw new IndexOutOfBoundsException();
    }
    Node<T> node = new Node<T>(null, elem, null);
    if(counter == 0) {
        first = last = node;
    }
    else {
        if(index == 0) {
            node.next = first;
            first.prev = node;
            first = node;
        }
        else if(index  == counter) {
            node.prev = last;
            last.next = node;
            last = node;
        }
        else {
            Node<T> current = this.first;

            for(int i = 0; i < index; i++) {
                current = current.next;
            }
            node.next = current;
            node.prev = current.prev;
            current.prev.next = node;
        }
    }
    counter++;
    return this;
}

用法示例#1(错误答案):

public class Node<T> {

public T data;
public Node<T> next;
public Node<T> prev;

public Node(Node<T> prev, T data, Node<T> next){
    this.data = data;
    this.next = next;
    this.prev = prev;
}

public Node() {

}

控制台:

之前:{h,e,a,p}

之后:{h,e,e,a,p}

控制台应该是什么:

之前:{h,e,a,p}

之后:{h,e,A,a,p}

1 个答案:

答案 0 :(得分:3)

current.prev = node;你在0 < index < counter

时丢失了这个

我测试你的代码,似乎有效:

public class LinkedList<T> {
    private Node<T> first;
    private Node<T> last;
    private int     counter;

    public LinkedList() {
    }

    public static void main(String args[]) {
        LinkedList<String> list = new LinkedList<String>();
        list.insertFirst("p");
        list.insertFirst("a");
        list.insertFirst("e");
        list.insertFirst("h");

        list.insertAt(2, "A");
        for (Node n = list.first; n != null; n = n.next) {
            System.out.println(n.data);
        }
    }

    private void insertFirst(T s) {
        insertAt(0, s);
    }

    public LinkedList<T> insertAt(int index, T elem) {
        if (index > counter) {
            throw new IndexOutOfBoundsException();
        }
        Node<T> node = new Node<T>(null, elem, null);
        if (counter == 0) {
            first = last = node;
        } else {
            if (index == 0) {
                node.next = first;
                first.prev = node;
                first = node;
            } else if (index == counter) {
                node.prev = last;
                last.next = node;
                last = node;
            } else {
                Node<T> current = this.first;

                for (int i = 0; i < index; i++) {
                    current = current.next;
                }
                node.next = current;
                node.prev = current.prev;
                current.prev.next = node;
                current.prev = node;
            }
        }
        counter++;
        return this;
    }
}

class Node<T> {

    public T       data;
    public Node<T> next;
    public Node<T> prev;

    public Node(Node<T> prev, T data, Node<T> next) {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }

    public Node() {

    }
}