深度复制(链接列表)

时间:2015-10-18 21:10:32

标签: java recursion linked-list deep-copy

这只是一个测试程序(我的原始程序从文件中获取数据,因此我省略了它,因为它可能使人们无法理解我的问题)

无论如何,我尝试深度复制我的对象数据,但我得到了一个" null"当我打印复制方法?这个代码有什么问题?这是我们如何使用递归深度复制?如果没有,任何深度复制的提示?还有其他方法可以将副本与递归区分开来吗?我并不完全确定我所做的事情是否正确,因为我正在从源头重复使用复制方法。

主要

public static void main(String[] args) {
    Person person = new Person();
    person.setFirstName("James");
    person.setLastName("Ryan");
    person.setAge(19);

    Person personTwo = new Person();
    personTwo.setFirstName("Steve");
    personTwo.setLastName("rivera");
    personTwo.setAge(22);

    LinkedList lList = new LinkedList();

    // add elements to LinkedList
    lList.add(person);
    lList.add(personTwo);


    //node
    Node str;

"变量str可能尚未初始化"

   //deep copy
    Node copy = Node.copy(str);
    System.out.println(copy);

}

LinkedList

class LinkedList {

// reference to the head node.
private Node head;
private int listCount;

// LinkedList constructor
public LinkedList() {
    // this is an empty list, so the reference to the head node
    // is set to a new node with no data
    head = new Node(null);
    listCount = 0;
}

public void add(Object data) // appends the specified element to the end of this list.
{
    Node Temp = new Node(data);
    Node Current = head;
    // starting at the head node, crawl to the end of the list
    while (Current.getNext() != null) {
        Current = Current.getNext();
    }
    // the last node's "next" reference set to our new node
    Current.setNext(Temp);
    listCount++;// increment the number of elements variable
}

public int size() // returns the number of elements in this list.
{
    return listCount;
}

public String toString() {
    Node Current = head.getNext();
    String output = "";
    while (Current != null) {
        output += "[" + Current.getData().toString() + "]";
        Current = Current.getNext();
    }
    return output;
}

}

节点

class Node {
// reference to the next node in the chain,
// or null if there isn't one.

Node next;
// data carried by this node.
// could be of any type you need.
Object data;

// Node constructor
public Node(Object dataValue) {
    next = null;
    data = dataValue;
}

// another Node constructor if we want to
// specify the node to point to.
public Node(Object dataValue, Node nextValue) {
    next = nextValue;
    data = dataValue;
}

// these methods should be self-explanatory
public Object getData() {
    return data;
}

public void setData(Object dataValue) {
    data = dataValue;
}

public Node getNext() {
    return next;
}

public void setNext(Node nextValue) {
    next = nextValue;
}

这是Node类

中的复制方法
public static Node copy(Node str) {
    if (str == null) {
        return null;
    }
    Node copyFirst = new Node(str.data, null);
    copyFirst.next = copy(str.next);
    return copyFirst;
}

}

class Person {

private String firstName;
private String lastName;
private int age;

public Person() {
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

//Overriding toString to be able to print out the object in a readable way
//when it is later read from the file.
public String toString() {

    StringBuilder buffer = new StringBuilder();
    buffer.append(firstName);
    buffer.append(" ");
    buffer.append(lastName);
    buffer.append(" ");
    buffer.append(age);
    buffer.append(" ");

    return buffer.toString();
}

由于

2 个答案:

答案 0 :(得分:0)

//dummy variable 
Node str = null;

//deep copy
Node copy = Node.copy(str);
System.out.println(copy);

你期待什么?

您需要复制列表,而不是某些虚拟节点。为此,LinkedList需要支持复制(或者至少是一种迭代其元素的方法)。 Node应该是LinkedList的用户完全隐藏的实现细节。

答案 1 :(得分:0)

浅层复制是指按原样重用节点值。例如。循环遍历您的节点,使新节点具有相同的值并将新节点链接在一起。您需要保留第一个作为新LinkedList对象的引用。

深度复制是指还克隆数据的时间。如果你的所有对象都实现了Cloneable,你就可以像上面描述的那样实现clone,而不是创建具有相同值的新节点,而只是复制新节点的值,并且你有一个深层拷贝。

相关问题