define a method addOn that it adds the data argument to the list in the sorted location in alphabetical. There can only be one of each company in this list.

public void addOn(company data)
{
Node it = new Node(data, head);

head = it;

it.data.toString();

}

this is a small method inside of my homework.
when I ran this it simply add nodes to the head NOT to the "sorted list"

I think there are 4 cases :
1) 0 nodes
2) 1 nodes
3) 3 nodes
4) more than 3 nodes


Thanks in advance

Your method will need to traverse the sorted linked list and find the appropriate place to insert it. Then it's a standard insertion by updating the "next" pointers so that the previous item in the list points to the new one and the new one points to the next one.

As written, you are only attaching a node to the head and then moving the head, which is going to result in a list of one at all times unless you are using a circularly linked list (which I doubt is the case).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.