質問

This is a homework assignment and im just stomped. Can anyone tell me what i am doing wrong?

I am trying to make a copy of a linked List but it says the new list is null.

I try debugging the code with appropriate prints, 1 printing the current node's data to be copyed, and the second to print the newList's last linked node to make sure it is being linked. They seem to be printing the right info, but the list is still null.

run:
h1 is 123456789
Expected: h1 is 123456789
1
2
tail digit:1
3
tail digit:2
4
tail digit:3
5
tail digit:4
6
tail digit:5
7
tail digit:6
8
tail digit:7
9
tail digit:8
h3 is null
Expected: h3 is 123456789
BUILD SUCCESSFUL (total time: 0 seconds)

UPDATE:

I did some further testing with this, and its actually not null and print the correct data,

if(newList.head != null || newList != null)
                {
                    System.out.println ("not null :D");
                    System.out.println(newList.toString());
                }

so that means h3 != newList that i made in the constructor. how do i set it so

HugeNumber h3 = new HugeNumber(h1);

h3 = newList throught the constructor.


public static void main(String[] args)
{
       // Create a HugeNumber that is 123456789
   HugeNumber h1 = new HugeNumber();
   for (int i=9; i>=1; i--)
   {
       h1.addDigit(i);
   }
   System.out.println("h1 is " + h1.toString());
       System.out.println("Expected: h1 is 123456789");

 // Make a copy of h1
   HugeNumber h3 = new HugeNumber(h1);
   System.out.println("h3 is " + h3.toString());
       System.out.println("Expected: h3 is 123456789");
}

class HugeNumber
{


/**
* Inner class to store a digit within a node
*/
private class DigitNode
{
    private int digit=0;            // Value for this digit
    private DigitNode next=null;    // Reference to next digit
    private DigitNode prev=null;    // Reference to previous digit
    /**
    * DigitNode constructor, initializes number
    */
    public DigitNode(int d)
    {
        digit = d;                        
            }
    /* Accessor and mutator methods */
    public int getDigit()
    {
        return digit;
    }
    public void setDigit(int d)
    {
        digit = d;
    }
    public DigitNode getNext()
    {
        return next;
    }
    public void setNext(DigitNode nextNode)
    {
        next = nextNode;
    }
    public DigitNode getPrev()
    {
        return prev;
    }
    public void setPrev(DigitNode prevNode)
    {
        prev = prevNode;
    }
}

// Variable declarations
private DigitNode head = null;  // Head points to the most significant digit in   the list
private DigitNode tail = null;      // Tail points to the least significant digit in the list


/**
* Constructors
*/
public HugeNumber()
{

}

/**
* addDigit adds a new digit, d, as a new most significant
* digit for the list.
* @param d new digit to add as the MSD
*/
public void addDigit(int d)
{
        DigitNode nodes = new DigitNode(d);
        if (head != null)
        {
            head.setPrev(nodes);
            nodes.setNext(head);
        }

        head = nodes;

        if(tail == null)
        {
            tail = nodes;
        }


}

/**
* Resets the HugeNumber to a null, empty value
*/
public void resetValue()
{
    head = null;
            tail = null;
}

/**
* @return String The HugeNumber converted to a string
*/
 public String toString() {
    if (head == null)
    {
        return null;              
    }

    String print = "";
    DigitNode temp = head;

    while (temp != null) {
        print = print + temp.getDigit();
        temp = temp.getNext();
    }
    return print;
}
    /**
* Deep copy constructor
* @param newVal Input HugeNumber to copy to this HugeNumber
    */

This constructor is where i make a copy of it.

public HugeNumber(HugeNumber newVal)
{
    // Traverse the input HugeNumber, copying each node to a new list
    //
            HugeNumber newList = new HugeNumber ();
            newList.head = newVal.head;                
            newList.tail = newList.head;  

            DigitNode currentNode = new DigitNode(1);
            DigitNode OldListNode = newVal.head;

            //to make sure the head is copied
            System.out.println(newList.head.digit);

            while(currentNode != newVal.tail)
            {
                OldListNode = OldListNode.next;
                currentNode = OldListNode;  

                //to make sure the currentNode is the one we want to copy and it is being transverse
                System.out.println(currentNode.digit);


                currentNode.setPrev(newList.tail);
                newList.tail.setNext(currentNode);
                newList.tail = currentNode;

                // to make sure the list is linked
                System.out.println("tail digit:" + newList.tail.prev.digit);
            }

                if(newList.head  == null || newList == null )
                {
                    System.out.println("--------------------------newList is null :(");
                }


}



}
役に立ちましたか?

解決

You just create new local variable newList in the constructor method to hold the data, and it will not exist after the invocation of constructor method. You should use this key word

他のヒント

The problem is in your overloaded constructor where you are creating a new object , which is unnecessary. You dont need to explicitly create an object there.The object will be created for you and you can access that using this reference.

Here is your modified constructor.

public HugeNumber(HugeNumber newVal) {
        // Traverse the input HugeNumber, copying each node to a new list
        //
        //HugeNumber this = new HugeNumber();
this();
        this.head = newVal.head;
        this.tail = this.head;

        DigitNode currentNode = new DigitNode(1);
        DigitNode OldListNode = newVal.head;

        // to make sure the head is copied
        System.out.println(this.head.digit);

        while (currentNode != newVal.tail) {
            OldListNode = OldListNode.next;
            currentNode = OldListNode;

            // to make sure the currentNode is the one we want to copy and it is
            // being transverse
            System.out.println(currentNode.digit);

            currentNode.setPrev(this.tail);
            this.tail.setNext(currentNode);
            this.tail = currentNode;

            // to make sure the list is linked
            System.out.println("tail digit:" + this.tail.prev.digit);
        }

        if (this.head == null || this == null) {
            System.out.println("--------------------------newList is null :(");
        }


    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top