title: Singly linked list Dictionary with intersection method
body: <p>I have to write a program in which I have to create a singly linked list dictionary in alphabetical order. Then have the words in the dictionary searched in a text file then do a intersect method to see if any word(s) intersect. So far I have created the text file as well as created the two nodes and done the mapping to search for how many times each word appears in this text file.  I am completely lost on how to create the singly linked dictionary. I was planning on creating two linked lists(Each with about 5 words in them) and then running an intersection alogirthm method. WOuld that be correct? Or is there an easier way to do it with the code I already have. Here is my code so far:</p>
  
  <pre><code>public class dictionary 
  {
    //variables
    dNode head;
    int size;
  
    //constructor
    public dictionary() 
    {
      head = null;
      size = 0;
    }
  
    //addFirst method
    public void addFirst(dNode s) 
    {
      s.setNext(head);
      head = s;
      size++;
    }
  
    public void addLast(dNode s)
    {
      if ( head == null )
      {
        head = s;
      }
      else
      {
        s.setNext(null);
        dNode w = head;
        while ( w.getNext() != null ) 
        { 
          w = w.getNext();
        }
        w.setNext(s);
      }
        size++;
    }
  
    //toString Method
    public String toString() 
    {
      String w = "";
      dNode s = head;
      while ( s != null ) 
      {
        w += s + "\n";
        s = s.getNext();
      }
      return w;
    }
  }
  
  
  
  
  public class dNode 
  {
    //variables
    String sent;
    posting post;
    dNode nextNode;
  
    //constructor
    public dNode(String sent, posting post, dNode nextNode)
    {
      this.sent = sent;
      this.post = post;
      this.nextNode = nextNode;
    }
  
    //returns element of this node
    public String getSent() {
      return sent;
    }
  
    //retunrs the next node of this node
    public dNode getNext() {
      return nextNode;
    }
  
    //modifier methods
    //sets elements of this node.
    public void setSent(String newSent) {
      sent = newSent;
    }
  
    //sets the next node of this node
    public void setNext( dNode newNext) {
      nextNode = newNext;
    }
    //toString method
    public String toString() 
    {
      return "Sentence and Posting: \n" + sent + "\n" + post;
    }
  }
  
  
  public class pNode {
    //variables
    int dID;
    String word;
    int occurence;
    pNode next;
  
    //constructor
    public pNode(int dID, String word, int occurence, pNode next)
    {
      this.dID = dID;
      this.word = word;
      this.occurence = occurence;
      this.next = next;
    }
    //return element of this node
    public String getWord() {
      return word;
    }
  
    //Returns the next node of this node
    public pNode getNext() {
      return next;
    }
  
    //Modifier methods
    //set the words of this node
    public void setWord(String newWord) {
      word = newWord;
    }
  
    //sets the next node of this node
    public void setNext(pNode newNext){
      next = newNext;
    }
  
    //toString method
    public String toString() {
      return "Document ID, Word, Occurence: \n " + dID + ", " 
        + word + ", " + occurence;
    }
  
  }
  
  public class posting 
  {
    //variables
    pNode head;
    int size;
  
    //constructor
    public posting() 
    {
      head = null;
      size = 0;
    }
  
    //addFirst method 
    public void addFirst(pNode s) 
    {
      s.setNext(head);
      head = s;
      size++;
    }
  
    //addLast method
    public void addLast(pNode s)
    {
      if ( head == null )
      {
        head = s;
      }
      else
      {
        s.setNext(null);
        pNode w = head;
        while ( w.getNext() != null ) 
        {
          w = w.getNext();
        }
        w.setNext(s);
      }
      size++;
    }
  
    //toString method
    public String toString()
    {
      String w = "";
      pNode s = head;
      while ( s != null) 
      {
        w += s + "\n";
        s = s.getNext();
      }
      return w;
    }
  }
  
  
  import java.io.*;
  import java.util.*;
  
    public class testFile
    {
  
    public static void main (String[] args) throws FileNotFoundException 
    {
      File filename = new File("/export/home/hawkdom2/s0878044/CS503/assignment2/sentences.txt");
      Scanner scan = new Scanner(filename);
      dictionary Dictionary = new dictionary();
  
     while ( scan.hasNextLine() )
     {
       String sentence = scan.nextLine();
       String[] word = sentence.split(" ");
  
       //first element is document id
       int dID = Integer.parseInt( word[0] );
  
       //insertion sort
       for ( int i = 2; i &lt; word.length; i++ )
       {
         for ( int j = i; j &gt; 1; j-- )
         {
          if ( word[j].compareTo( word[j-1] ) &gt; 0 )
          {
            String switchs = word[j];
            word[j] = word[j-1];
            word[j-1] = switchs;
          }
         }
       }
  
       //integer array count
       int[] count = new int[word.length];
       for ( int i = 1; i &lt; word.length; i++)
       {
         for ( int j = 1; j &lt; word.length; j++)
         {
           if (word[i].equalsIgnoreCase( word[j] ) )
           {
             count[i]++;
           }
         }
       }
  
       posting posts = new posting();
  
       for ( int i = 1; i &lt; word.length; i++ )
       {
         if ( (i &gt; 1 ) &amp;&amp; (word[i].equalsIgnoreCase( word[i-1] ) ) )
           continue;
         else
         {
           posts.addFirst(new pNode(dID, word[i], count[i], null) );
         }
       }
  
       Dictionary.addLast(new dNode(sentence, posts, null) );
     }
  
   String[]words ={"cat", "chased", "dogs", "mammals", "roses"};
     LinkedList&lt;String&gt; list1 = new LinkedList&lt;String&gt;();
      for (String x : words)
    list1.add(x);
  
      String[] words2 = {"dislikes", "favorite", "likes", "pink", "red"};
      LinkedList&lt;String&gt; list2 = new LinkedList&lt;String&gt;();
      for (String y : words2)
        list2.add(y);
  
     //print out output
     System.out.println(Dictionary);
    }
    }
  </code></pre>
  
  <p>And this is my text file containing the sentences:</p>
  
  <pre><code>1 a rose is a rose 
  2 John chased a cat and the cat chased John
  3 cats are mammals but mammals are not cats
  4 beavers build dams but i know a beaver that does not
  5 my dog chased a cat and the cat attacked my dog
  6 my dog likes cats but my cat dislikes dogs
  7 my dog likes roses but roses dislike my dog
  8 my cat dislikes roses but roses like my cat
  9 red roses are not my favorite roses
  10 my favorite roses are pink roses
  </code></pre>