- #1
apiwowar
- 96
- 0
having trouble with a copy constructor for a linked list. I know what I am doing wrong I am just not too sure how to fix it. First i'll show my node class and the copy constructor that I am working on.
node class:
and the copy constructor
the constructor is in a class called CharList and the only variable in the class is a CharNode head which represents the pointer.
the problem is that the CharNode constructor takes in a char and a CharNode but in my constructor I am passing in nothing.
would it be a good idea to create the new CharNode inside the while loop or am i just doing it the wrong way?
node class:
Code:
class CharNode
{
private char letter;
private CharNode next;
public CharNode(char ch, CharNode link)
{
letter = ch;
next = link;
}
public void setCharacter(char ch)
{
letter = ch;
}
public char getCharacter()
{
return letter;
}
public void setNext(CharNode next)
{
this.next = next;
}
public CharNode getNext()
{
return next;
}
}
and the copy constructor
Code:
// copy constructor
public CharList(CharList l)
{
CharNode pt = head;
CharNode newList = new CharNode();
while(pt.getNext() != null)
{
newList.setCharacter() = l.getCharacter();
newList.setNext() = l.getNext();
}
}
the constructor is in a class called CharList and the only variable in the class is a CharNode head which represents the pointer.
the problem is that the CharNode constructor takes in a char and a CharNode but in my constructor I am passing in nothing.
would it be a good idea to create the new CharNode inside the while loop or am i just doing it the wrong way?