单一链表字典与交集方法

时间:2013-11-25 00:42:29

标签: java dictionary intersection singly-linked-list

我必须编写一个程序,我必须按字母顺序创建一个单链表。然后在文本文件中搜索字典中的单词,然后执行交叉方法以查看是否有任何单词相交。到目前为止,我已创建了文本文件,并创建了两个节点并完成了映射,以搜索每个单词在此文本文件中出现的次数。我完全迷失了如何创建单链字典。我打算创建两个链表(每个链表中包含大约5个单词),然后运行交叉alogirthm方法。这是正确的吗?或者有一种更简单的方法来使用我已有的代码。到目前为止,这是我的代码:

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 < word.length; i++ )
     {
       for ( int j = i; j > 1; j-- )
       {
        if ( word[j].compareTo( word[j-1] ) > 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 < word.length; i++)
     {
       for ( int j = 1; j < word.length; j++)
       {
         if (word[i].equalsIgnoreCase( word[j] ) )
         {
           count[i]++;
         }
       }
     }

     posting posts = new posting();

     for ( int i = 1; i < word.length; i++ )
     {
       if ( (i > 1 ) && (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<String> list1 = new LinkedList<String>();
    for (String x : words)
  list1.add(x);

    String[] words2 = {"dislikes", "favorite", "likes", "pink", "red"};
    LinkedList<String> list2 = new LinkedList<String>();
    for (String y : words2)
      list2.add(y);

   //print out output
   System.out.println(Dictionary);
  }
  }

这是我的文本文件,包含句子:

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

0 个答案:

没有答案