java - 如何在循环链表中插入字符串(我想检查循环链表中的重复单词)?

标签 java string methods circular-dependency

这是我创建的链接类,我编写了一个方法来检查链接列表中是否有重复的单词。我尝试将字符串发送到 addFirst 方法,但我不知道为什么它对我不起作用

class LinkedList<String> 
{ 


private class Node<String> 
{ 
      private String word; // reference to the element stored at this node
      private Node<String> next; // reference to the subsequent node in the list
      public Node(String w, Node<String> n) 
      { 
        word = w;
        next = n;
      } 


      public String getWord( ) { return word; } 
      public Node<String> getNext( ) { return next; } 
      public void setNext(Node<String> n) { next = n; } 
  } 

 private Node<String> head = null; // head node of the list (or null if empty)
 private Node<String> tail = null; // last node of the list (or null if empty)
 private int size = 0; // number of nodes in the list

 public LinkedList( ) { }
 public int size( ) { return size; } 
 public boolean isEmpty( ) { return size == 0; } 

 public Node<String> getHead( ) 
 { // returns  the head node
   if (isEmpty( )) return null;
   return head;
 } 


 public void addFirst(Node<String> w) 
 { Node<String> newest;
        newest= w;
        tail.next=newest;
        newest.next=head;
         size++;
 } 
 public void addLast(Node<String> w){
       Node<String> newest;
       newest=w;
       tail.next=newest;
       newest.next=head;
            size++;

 } 

 public String last( ) 
      { // returns (but does not remove) the last element
             if (isEmpty( )) return null;
         return tail.getWord( );
 }

 public boolean checkDuplicate(Node<String> w) {
     Node temp;
     for(Node a=tail.next;a !=null;a=a.next){
       temp=a;
       for(Node b=temp.next;b != null;b=b.next){
          if(b.equals(temp.next))
              return true;
      }
     }
     return false;
   }
}

主要是我无法将单词插入循环链表

public class Duplicate {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    LinkedList<String> list = new LinkedList<String>(); 


    // the prblem start from here
            list.addFirst("world");
            list.addFirst("world");
            list.addFirst("will");
            list.addFirst("be");
            list.addFirst("will");
            list.addFirst("a better");
            list.addFirst("place");
            //to here
             System.out.println(list.checkDuplicate(list.getHead()));
}
}

最佳答案

抱歉 - 很多事情都是错误的。这是代码,请研究一下您的差异。要初始化私有(private)类变量,您必须使用构造函数。您的 addFirst() 方法必须采用 String 而不是 Node 对象。输出为:

-检查节点“位置” -检查节点“更好” -检查节点“意愿” -第一个重复项是“will” -true

public class LinkedTest   // Realy want a circulare linked list or a normal linked list ???
{
    private MyNode head; // head node of the list (or null if empty)
    private MyNode tail; // last node of the list (or null if empty)
    private int size; // number of nodes in the list   

private class MyNode<>  // INNER class
{ 
    private String word; // reference to the element stored at this node
    private MyNode next; // reference to the subsequent node in the list

    public MyNode(String w, MyNode n) 
     { 
       word = w;
       next = n;
     } 
// The outer class has access to the variables of the inner
//     public String getWord( ) { return word; } 
//     public MyNode<String> getNext( ) { return next; } 
//     public void setNext(MyNode<String> n) { next = n; } 
} // end of inner class



public LinkedTest( ) { head = null; tail=null; size=0; } // constructor
public int size( ) { return size; } 
public boolean isEmpty( ) { return size == 0; } 
public MyNode getHead( ) { return head; }

public void addFirst(String w) 
{
    MyNode n = new MyNode(w,null);
     if (head == null)
     {  head = n; tail = n; 
     } else
     {
        //n.setNext(head);
        n.next = head;
        head = n;
     }
     size++;
} 
public void addLast(String w)
{
      MyNode n = new MyNode(w,null);
      if (tail != null)
      {   tail.next = n; // tail.setNext(w);
          tail = n;
      } else
      {  head = n;
         tail = n;
      }
      size++;
} 

 public String last( ) 
 { // returns (but does not remove) the last element
      return  tail == null ? null : tail.word;
 }

/**
 * Checks the linked list for at least one duplicate
 * @return true if a duplicte was found, false otherwise
 */
public boolean checkDuplicate() 
{
    String str;

    for (MyNode a = head;a != null; a = a.next)
    { 
         str = a.word;
         System.out.println("Examine node '" + str + "'");

         for (MyNode b = a.next;b != null;b = b.next) // scan the list behind 'a'
         {
             if (str.equals(b.word))
             {    
                 System.out.println("First duplicate is '" + str + "'");
                 return true;
             }
         }
    }
    return false;
} //---------------- checkDuplicate

}//LinkedTest 类结束

关于java - 如何在循环链表中插入字符串(我想检查循环链表中的重复单词)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005945/

相关文章:

swift - 计算总距离 swift iOS

java - 代码厨师 : Holes in the Text

css - css 属性 "top"是 javascript 中的字符串还是数字?

java - 从 JSP 生成 HTML

string - 如何使用 ack 搜索字符串 '--branch'?

C++ 段错误追加、push_back 和运算符 = 或 +=

java - javac 是否应该在同名的匿名类之外查找方法?

java - 从给定整数数组中查找最接近中间范围的数字的方法

java - vlcj如何控制音量?

java - 有什么简单的方法可以调整一段文字的字体大小以适应特定区域?