-2

This is just a test program (my original program gets data from a file so i omitted that since it might complicate people from understanding my problem)

Anyways, I tried deep copying my object data but I'm get a "null" when i print the copy method? what's wrong with the code? is this how we deep copy using recursion? if not, any tips to deep copying? Any other ways to keep copy apart from recursion? I'm not entirely sure if what I'm doing is right honestly since i'm reusing the copy method from a source.

Main

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;

"Variable str might not have been initialized"

   //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;
}

}

Node

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;
}

Here is the copy method within the Node class

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;
}

}

Person

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();
}

Thanks

Mathy
  • 111
  • 2
  • 6

2 Answers2

0
//dummy variable 
Node str = null;

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

What did you expect?

You need to copy the list, not some dummy node. For that, LinkedList needs to support copying (or at least a way to iterate over its elements). Node should be an implementation detail completely hidden from the users of LinkedList.

Svante
  • 50,694
  • 11
  • 78
  • 122
0

A shallow copy is when you reuse the node values as is. eg. you loop through your Nodes making new Node with the same values and chaining the new nodes together. You'll need to keep the first as the reference for the new LinkedList object.

A deep copy is when the data is also cloned. If all your objects implements Cloneable you just implement clone as describes above and instead of making new node with the same value you just make a clone of the value for the new Node and voilà, you got a deep copy.

Sylwester
  • 47,942
  • 4
  • 47
  • 79