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?

Thanks

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){
    next=null;
    data=d;
   }

public MyLinkedList(String d,MyLinkedList m){
    next=m;
    data=d;
   }

  public void setNext(MyLinkedList n){
   next=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){
  if(m.next==null)
    return count++;

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

/*
The first call would be
countCharacters(m,0);
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"

And do your own homework. Why should we do your work for you if you're too lazy to do it yourself?

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.