0

I have 2 questions about linked lists so i figured i'll post them in one question.

first i'll show my node class and the copy constructor and constructor from a string

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

}  

copy constructor

   // copy constructor  
   public CharList(CharList l) 
{
    CharNode pt = head;

    while(pt.getNext() != null)
    {
        this.setCharacter() = l.getCharacter();
        this.setNext() = l.getNext();
    }
} 

constructor from string

   // constructor from a String 
   public CharList(String s) 
{ 
    head = head.setCharacter(s);


}

when i try to compile i get an error for my copy constructor it says that it cant find the symbol this.setCharacter()... and the l.setCharacter()...

am i just doing it completely wrong?

and with my constructor from a string i know thats wrong. i thought about using the charAt() but how would i know when to stop the loop to do that? is that a good approach to take?

any help would be appreciated.

3
  • 1
    You have received answers for 7 questions to date and not accepted any of the answers. You will find that people are less inclined to help if you do not accept good answers. Commented Oct 14, 2011 at 23:36
  • you have to read a bit about the concept of encapsulation , if you do u'll resolve the compilation problemes yourself Commented Oct 14, 2011 at 23:38
  • you have to accept an answer? Commented Oct 14, 2011 at 23:59

4 Answers 4

1

in your CharList constructor, this refers to the CharList class, which doesn't have a setCharacter() method (CharNode does). also, when you call a method in java, you need to pass the parameter, e.g. setFoo(newFoo), not setFoo() = newFoo

Sign up to request clarification or add additional context in comments.

2 Comments

can i say something like CharNode.setCharacter(paramater)?
that's the basic idea, although you would generally be working with an instance of CharNode, not the class itself.
0

Your set character method is probably in your node, not in your list. You also need to be moving your pointer along. What I mean is, where do you ever "go to the next node"?

Comments

0

Your copy constructor is for the class CharList, while setCharacter is defined in CharNode.

this in the copy constructor references the current instance of the CharList object the constructor is defined on. l is also a CharList in your code rather than a CharNode which defines setCharacter.

The copy constructor should be defined in the CharNode class.

Comments

0

In your "copy constructor" you would need to go through the list being passed in starting with its head, and create new nodes for your new list ...

public CharList(CharList l) 
{
    // Whatever method your CharList provides to get the 
    // first node in the list goes here
    CharNode pt = l.head(); 

    // create a new head node for *this* list
    CharNode newNode = new CharNode();
    this.head = newNode;

    // Go through old list, copy data, create new nodes 
    // for this list.
    while(pt != null)
    {
        newNode.setCharacter(pt.getCharacter());
        pt = pt.getNext();
        if (pt != null)
        {
            newNode.setNext(new CharNode());
            newNode = newNode.getNext();
        }

    }
} 

As for creating a list from a String ... same concept except you iterate through the string rather than another CharList

for (int i = 0; i < myString.length(); i++)
{ 
    ...
    newNode.setCharacter(myString.charAt(i));
    ...

4 Comments

strings have lengths? is it like an array has length?
string is array of characters.
i have to create a new charnode inside the constructor, is that right?
@alexthefourth - Yes, the String object provides a length() method. You may want to read the javadoc to see all the other things it has as well. As for your second question, well ... yes. How else would you construct a new list? The above code is complete and would create a new CharList object with the same contents as the one passed in. You would construct a new list from a string in the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.