Title: SortedList.java
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

SortedList.java


// This class maintains a Sorted List of WordInfo classes.
// The first criterea of the sort is the Word in WordInfo.
// If classes have the same word, then the line number breaks
// the tie.

// To learn about how to use this class, execute:
//
// java SortedList
//
// The main routine will demo how to use this class with a little
// example.

// Note that SortedList has a toString method that dumps out a
// printout of the SortedList contents.


public class SortedList
{
   WordInfo head=null;

   // The next routine can be used to obtain the next element in
   // the list.  To start at the beginning, pass in a null for the
   // one parameter.  When a null is returned, you are at the end
   // of the list


   public WordInfo next(WordInfo w)
   {
        if (w == null) return head;
        else return w.next;
   }

   // This routine not only dumps the contents of the SortedList,
   // but it also shows you how to create a loop to look at the
   // individual objects.


   public String toString()
   {
        WordInfo w ;
        StringBuffer sb = new StringBuffer();
        // calling next(null) returns first WordInfo entry in the SortedList
        // calling next(w) returns the next WordInfo entry if w is the current entry

        for(w=next(null);w != null; w=next(w))
        {
            sb.append(w+" ");
        }
        return sb.toString();
   }

   // The routine allows you to add new items to the Sorted List
   // Don't worry about the details about how it works.
   // The key thing is that the list is stored in sorted order.

   public void add(String word, int line)
   {
        WordInfo w2Insert = new WordInfo(word,line);
        if (head == null)
        {
            head = w2Insert;
            return;
        }
        int d = head.compareTo(w2Insert);
        if (d > 0)
        {
            w2Insert.next = head;
            head = w2Insert;
            return;
        }

        WordInfo w ;
        WordInfo wnext;
        for(w=head;;w=wnext)
        {
           wnext = w.next;
           if (wnext == null)
           {
                w.next = w2Insert;
                return;
           }
           d = w.compareTo(w2Insert);
           if (d==0)
           {
                w.count += 1;
                return;
           }
           int d2 = wnext.compareTo(w2Insert);
           if (d2 ==0)
           {
                wnext.count += 1;
                return;
           }
           if (d < 0 && d2 > 0)
           {
              w2Insert.next=wnext;
              w.next=w2Insert;
              return;
           }

        }
   }

   public static void main(String[] args)
   {
        SortedList list = new SortedList();
        // Study this code to see how to use the SortedList class

        list.add( "zebra",5);
        list.add( "melon",5);
        list.add( "orange",1);
        list.add( "apple",2);
        list.add( "belly",3);
        list.add( "melon",2);
        list.add( "pop",1);
        list.add( "melon",2);
        list.add( "cat",3);
        list.add( "zebra",5);


        // You can create a loop to
        // walk through the SortedList as follows:

        WordInfo w, last=null;

        for (w=list.next(null); w != null; w = list.next(w))
        {
            boolean same = w.sameWord(last);
            if (same)
                System.out.println(w.word + " line="+w.line);
            else System.out.println("New Word: "+ w.word + " line="+w.line);
            last = w;
        }
   }

}