Skip to content
Advertisement

How to insert a string in a circular linked list (i want to check the duplicate words in the circular linked list)?

Here is the linked class that I created and I wrote a method that checked if there was a duplicate words in my linked list. I tried to sent the string to the addFirst method but I don’t know why it doesn’t work with me

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;
   }
}

in the main i can’t insert the words to the circular linked list

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()));
}
}

Answer

sorry – a lot of things are wrong. Here is the code pleas study the difference to your. To initialize private class variables you must use the constructor. Your addFirst() method must take a String instead of a Node object. The output is:

-Examine node ‘place’ -Examine node ‘a better’ -Examine node ‘will’ -First duplicate is ‘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

} // end of class LinkedTest

Advertisement