Hey, I have to write a Java program that implements a sorted linked list to be able to count the number of chars and words in an input file.

would any1 be able to help me please?


Well, I really think its stupid if you *have to* do that program; there are more efficient ways to count the number of chars in a file, and better examples of using linked lists...

First of all, you should know there is a Java class in the JDK called LinkedList, but I suposse you have to create your own right?

If thats the case, it should be something like this

public class MyLinkedList{
   private MyLinkedList next; //Will hold next member of the list
   private String data; //Will hold the character or whatever info

//Two constructors depending on your implementation
  public MyLinkedList(String d){

public MyLinkedList(String d,MyLinkedList m){

  public void setNext(MyLinkedList n){

 public MyLinkedList getNext(){
   return next;

 public String getData(){
  return data;

And you'd also have to make a function to rotate throught the linked list
Something among the lines of

public int countCharacters(MyLinkedList m,int count){
    return count++;

  //Else, but java compiler would cry like a little girl
      return countCharacters(m.next,count++);

The first call would be
Where m is a global variable that contains the LinkedList itself (aka is the root node...

And notice this was just a single LinkedList, you can have double ones, with a reference to the former element

Next time take sometime to make something first, and not just post "Hey, do my homework please"

This article has been dead for over six months. Start a new discussion instead.