如何在循环链表中插入一个字符串(我想检查循环链表中的重复单词)?
How to insert a string in a circular linked list (i want to check the duplicate words in the circular linked list)?
这是我创建的链接 class,我写了一个方法来检查我的链接列表中是否有重复的单词。我试图将字符串发送到 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()));
}
}
抱歉 - 很多地方都错了。这是代码请研究你的区别。要初始化私有 class 变量,您必须使用构造函数。您的 addFirst() 方法必须采用 String 而不是 Node 对象。输出是:
-检查节点'place'
- 检查节点 'a better'
- 检查节点 'will'
-第一个副本是 'will'
-真
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
} // class LinkedTest
结束
这是我创建的链接 class,我写了一个方法来检查我的链接列表中是否有重复的单词。我试图将字符串发送到 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()));
}
}
抱歉 - 很多地方都错了。这是代码请研究你的区别。要初始化私有 class 变量,您必须使用构造函数。您的 addFirst() 方法必须采用 String 而不是 Node 对象。输出是:
-检查节点'place' - 检查节点 'a better' - 检查节点 'will' -第一个副本是 'will' -真
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
} // class LinkedTest
结束