title: concatenation of two doubly linked list in Java
body: <p>Hi am trying to Concatenate two doubly link list together.  The code that i have does work but not sure of it is programmatic correct.  The concat method is to join to doubly linked list.  Like for example if have linkedString t =  new linkedString(&quot;Super&quot;) and another linkedString s = new linkedString = new linkedString(&quot;man&quot;)  and will be invoked  by doing linkedString newString = t.concat(s); and the value for concat should be superman. But I am not sure if I had done it correctly.  The question is how to take full advantage of the double-linked list representation.</p>
  <pre><code>    /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */
       package project2;
  
  import static project2.Project2.*;
  
  
  /**
   *
   * @author denge
   */
  
  public class LinkedString &lt;T&gt; implements LinkedStringInterface &lt;T&gt; {
  
  
  
      private class Node &lt;T&gt; {
  
          protected T data; // the value of the data itself
          protected Node &lt;T&gt; next;// // next current in the list
          protected Node &lt;T&gt; previous;  // link to previous current
  
          public T getData() {
              return data;
          }
  
          public void setData(T data) {
              this.data = data;
          }
  
          public Node getNext() {
              return next;
          }
  
          public void setNext(Node &lt;T&gt; next) {
              this.next = next;
          }
  
          public Node  getPrevious() {
              return previous;
          }
  
          public void setPrevious(Node &lt;T&gt; previous) {
              this.previous = previous;
          }
  
          public Node(T value) {
              this.data = value;
              this.next = null;
              this.previous = null;
          }
  
          public Node(T value, Node &lt;T&gt; newNext , Node &lt;T&gt; newPrevious) {
              this.data = value;
              this.next = newNext;
              this.previous = newPrevious;
          }
  
          @Override
          public String toString() {
  
              return data.toString();
          }
  
      }
  
      private Node &lt;T&gt; last; // tail of list
      private Node &lt;T&gt; first; //head of list
      private int counter; // keep track number of elements in the linked list. 
  
      /**
       * constructors to initialize linklist counter to zero
       */
      public LinkedString() {
          counter = 0;
      }
  
      public LinkedString(char[] value)
      {
          for (int index = 0; index&lt;value.length;index++)
          {this.addToEnd(value[index]);
          }
      }
  
      public LinkedString(String original){
  
          for (int index = 0; index &lt;original.length();index++)
          {this.addToEnd(original.charAt(index));
          }
  
  
      }
      public char charAt(int index)throws LinkedStringException{
          if (index &lt;0 || index&gt;length())
          {thowExeption(index);}
        Node &lt;T&gt; current = this.find(index);
  
        return (char) current.data;
      }
  
      public LinkedString&lt;T&gt; concat(LinkedString str) throws LinkedStringException
      {
  
  
          if(isEmpty())
          {
              throw new LinkedStringException(&quot;List is Empty&quot;);
          }
  
  
  
          LinkedString&lt;T&gt; tempString = new LinkedString(this.toString() + str.toString());
                 
          return tempString;
      }
  
      public LinkedString&lt;T&gt; substring(int beginIndex, int endIndex) throws LinkedStringException{
          if(isEmpty())
          {
              throw new LinkedStringException(&quot;Current List is empty.&quot;);
  
          }
  
          if (beginIndex&lt;0)
          {
              thowExeption(beginIndex);
          }
          if (endIndex&gt;length())
          {
              thowExeption(endIndex);
          }
          var current = this.find(beginIndex);
          String tempString2=&quot;&quot;;
          int tCounter=beginIndex;
          while (current!=null&amp;&amp;tCounter&lt;endIndex+1)
          {
              tempString2 = tempString2 + current.data;
              current=current.next;
              tCounter++;
          }
          var str = new LinkedString(tempString2);
          return str;
      }
  
      /**
       * Puts value at the start of the list
       *
       * @param value Value to be added at the beginning of the list.
       */
          private void addToFirst(char value) {
  
                 Node &lt;T&gt; current = new Node(value);
          if (isEmpty()) {
              first = last = current;
              first.previous=null;
              last.previous=null;
          } else {
              first.previous = current;
              current.next = first;
              first = current;
              first.previous = null;
          }
          
          counter++;
          
      
      }
      /**
       * Appends value to the end of the list
       *
       * @param value Value to be added at the end of the list.
       */
      private void addToEnd(char value) {
  
          Node node = new Node(value);
          if (isEmpty()) {
              first = last = node;
               first.previous=null;
               last.previous=null;
          } else {
              last.next = node;
              node.previous = last;
              last = node;
              last.next=null;
          }
          counter++;
  
  
  
      }
  private void thowExeption(int index)
  {
      if (index&lt;0)
      {
          throw new LinkedStringException(&quot;Beginning Index can not be less than 0. Beginning index provided &quot; + index);
      }
      if (index&gt;length())
      {
          throw new LinkedStringException(&quot;Ending Index can not be greater than &quot; + length() +&quot;. Ending index provided &quot; + index);
      }
  
  }
  
      /**
       * Searches the entire list to check to see if value is in the list.
       *
       * @param value The data is be compared with each item in the list.
       * @return Returns true if value of the data exists in the list. Returns
       * false if the value provided does not exists in the list.
       *
       */
      @Override
  
      public boolean contains(T value) {
  
          Node &lt;T&gt; current = first;
          boolean isFound = true;
          /*try {
              indexOf(value);
          } catch (LinkedStringException ex) {
              isFound = false;
          }*/
  
  //        while (current != null) {
  //            if (current.data == value) {
  //                isFound = true;
  //                return isFound;
  //            }
  //            current = current.next;
  //        }
          return isFound;
      }
  
  
         
      private Node getPrevious(Node &lt;T&gt; node) {
          var current = first;
          while (current != null) {
              if (current.next == node) {
                  return current;
              } // end if statement             
              current = current.next;
          } // end of while loop
  
          return null;
  
      }
  
      /**
       * Method converts the list to an Array. 
       * @return Returns an char Array of values
       */
  
      /**
       * Prints out the items of the linked list.
       */
      public void printList() {
          println(&quot;\n-------------------Printing in the Forward direction------------------&quot;);
          var current = first;
          println(&quot;Number of Items that are in the list: &quot; + length());
          //println(&quot;first -&gt; &quot;);
          while (current != null) {
              System.out.print(current);
              System.out.print(&quot; &quot;);
              current = current.next;
  
          }
          println();
            println(&quot;\n-------------------Printing in the reverse direction------------------&quot;);
           current = last;
          println(&quot;Number of Items that are in the list: &quot; + length());
          //println(&quot;first -&gt; &quot;);
          while (current != null) {
              System.out.print(current);
              System.out.print(&quot; &quot;);
              current = current.previous;
  
          }
          println();
      }
      public String toString(String direction){
          String printedList=&quot;&quot;;
       
       if (direction.equalsIgnoreCase(&quot;forward&quot;)){
           var current=first;
       while(current!=null)
       {
       printedList = printedList + current.toString() + &quot; &quot;;
       current = current.next;
       }}
       
          if (direction.equalsIgnoreCase(&quot;reverse&quot;)){
              var current=last;
       while(current!=null)
       {
       printedList = printedList + current.toString() + &quot; &quot;;
       current = current.previous;
       }}
       
       
  return printedList;
          
      
      }
      
     
      
      @Override
      public String toString(){
       String printedList=&quot;&quot;;
       var current=first;
       while(current!=null)
       {
       printedList = printedList + current.toString();
       current = current.next;
       }
       
  return printedList;
  }
  
      /**
       *
       * @return Returns True if linked list is empty
       */
      @Override
      public boolean isEmpty() {
  
  // returns true if empty and false if not empty
          return counter == 0;
  
      }
  
  
  
  
      /**
       * Returns number of items in the list. For example if the list contains 3 2
       * 1 0 the return value is 4
       *
       * @return Size of the list.
       */
      @Override
      public int length() throws LinkedStringException {
  if (isEmpty()){
      throw new LinkedStringException(&quot;List is empty.&quot;);
  }
  
  
          return (counter);
      }
  
      /**
       * Clears out entire content of the array.
       */
  
      public void removeAll() {
          if (isEmpty() == false) {
              first = last = null;
              counter = 0;
          }
      }//clears the entire link list. 
  
      /**
       * @throws LinkedStringException Throws an exception if index provided is Out of
       * Bounds or out of range.
       * @param index Where in the list to retrieve the data.
       * @return Returns the data store at a given index.
       *
       */
      @Override
      public T get(int index) throws LinkedStringException {
          if (index &lt; 0 || index &gt;= counter) {
              thowExeption(index);
          }
        var current = find(index);
        
          return (T) current.data;
      }
  
      /**
       * Returns the first value of the linked list.
       *
       * @return
       */
  
      private Node &lt;T&gt; find(int index) {
          var current = first;
          for (int i = 0; i &lt; index; i++) {
              current = current.next;
          }
          return current;
      }
  
  
      /**
       * Compares the Values of two list. 
       * 
       * @param List Second list to be compared. 
       * @return
       * Returns true is both lists are the sames size all and values are equal to 
       * each other.  If one sets of data are not equal then method will return false.
       */
      public boolean equals (LinkedString List) throws LinkedStringException
      {
          if (this.isEmpty()&amp;&amp;List.isEmpty())
          {
          throw new LinkedStringException(&quot;Unable to Compare due to both Lists being empty.&quot;);
          }
          if(this.isEmpty())
          {
        
          throw new LinkedStringException(&quot;Unable to Compare due to an first list being empty.&quot;);
          }
         if (List.isEmpty())
         {
             throw new LinkedStringException(&quot;Unable to Compare due to an Second list being empty.&quot;);}
          
          println(&quot;Comparing two link lists.&quot;);
          var current = first;
          var tempCurrent = List.first;
        
          if (this.counter != List.counter)
          {
          return false;
          }
          while (current!=null){
               println(current + &quot; &quot; + tempCurrent );
  
            if (current.data != tempCurrent.data)
           {   
               return false;}
               current = current.next;
               tempCurrent=tempCurrent.next;
           } 
      return true;
      }   
  }
  
  /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package project2;
  
  //import java.util.LinkedList;
  //import java.util.Scanner;
  import java.util.*;
         
  
  
  /**
   *
   * @author denge
   */
  public class Project2 {
  
      /**
       * Prints out to the console with a newline character. 
       * @param x
       */
      public static final void println(Object x) { System.out.println(x); }
      
      /**
       * Method prints a blank line. 
       */
      public static final void println() { System.out.println(); }
  
      /**
       * Printing out to the console. 
       * @param x data that needs to be printed out
       */
      public static final void print(Object x) { System.out.print(x); }
      public static void main(String[] args) {
                  
          Scanner input = new Scanner(System.in);
  
          int index = 0;
          char[] data = {'S','u','p','e','r'};
          char[] data2 = {'B','o','y'};
          LinkedString &lt;Character[]&gt; secondCList = new LinkedString&lt;&gt;(data2);
          LinkedString &lt;Character[]&gt;  firstCList  = new LinkedString(data);
          LinkedString &lt;String&gt; firstSList = new LinkedString(&quot;Wonder&quot;);
          LinkedString &lt;String&gt; secondSList = new LinkedString(&quot;Woman&quot;);
          
          LinkedString &lt;String&gt; tempList = firstSList.concat(secondCList);
          //  tempList = firstSList.concat(secondCList);
         
  
  
          
          println(&quot;----Printing out table testing out length method--------&quot;);
  
          println(&quot;LinkedList Names        Contents          Length&quot;);
          println(&quot;------------------------------------------------&quot;);
          println(&quot;firstCList\t\t\t\t&quot; +  firstCList.toString()+&quot;\t\t\t\t&quot;+firstCList.length());
          println(&quot;secondCList\t\t\t\t&quot; +  secondCList.toString()+&quot;\t\t\t\t\t&quot;+secondCList.length());
          println(&quot;firstSList\t\t\t\t&quot; +  firstSList.toString()+&quot;\t\t\t\t&quot;+firstSList.length());
          println(&quot;secondSList\t\t\t\t&quot; +  secondSList.toString()+&quot;\t\t\t\t&quot;+secondSList.length());
          println(&quot;tempList(concat)\t\t&quot; +  tempList.toString()+&quot;\t\t\t&quot;+tempList.length());
  
          println(&quot;------testing out charAt method----------&quot;);
          println(&quot;\nThe 3 character of firstCList = &quot;+  firstCList.charAt(2));
          println(&quot;The 2 character of firstSList = &quot;+ firstSList.charAt(1));
          println(&quot;Testing out the get() at value 3 of secondSList: &quot;+(Object) secondSList.get(2));
  
          println(&quot;\n----testing out conatenation method -----------&quot;);
  
          println(&quot;Concatenating firstCList with secondCList &quot; + firstCList.concat(secondCList));
          println(&quot;Concatenating firstSList with secondSlist &quot; + firstSList.concat(secondSList));
          println(&quot;Concatenating firstSList with secondCList &quot; + firstSList.concat(secondCList));
          println(&quot;Concatenating firstClist with secondSList &quot; + firstCList.concat(secondSList));
  
          println(&quot;\n---Testing out Substring Method----&quot;);
          println(&quot;Printing the 2nd to 3rd character from firstCList: &quot;+firstCList.substring(1,2));
          println(&quot;Printing the 1st to 3rd character from firstSList: &quot;+firstSList.substring(0,2));
          println(&quot;Printing the 2 to 7 character from tempList: &quot;+tempList.substring(1,6));
  
  
          println(&quot;\nNow testing to see if the exception handling is working. clearing out firstCList&quot;);
          println(&quot;Is firstCList empty: &quot; + firstCList.isEmpty());
          println();
          firstCList.removeAll();
          println(&quot;Cleared firstCList. Is firstCList empty: &quot; + firstCList.isEmpty());
          println();
          try{
              firstCList.length();
          }
  
          catch (LinkedStringException ex)
          {
              println(&quot;Exception handling is working. &quot; + ex.getMessage());
          }
  
          firstCList  = new LinkedString(data);
          try{
              println(&quot;Printing the 2 and 3 character from firstCList: &quot;+firstCList.substring(1,10));
          }
          catch (LinkedStringException ex)
          {
              println(&quot;Exception handling is working. &quot; + ex.getMessage());
          }
  
          try{
              println(&quot;Printing the 2 and 3 character from firstCList: &quot;+firstCList.substring(-1,10));
          }
          catch (LinkedStringException ex)
          {
              println(&quot;Exception handling is working. &quot; + ex.getMessage());
          }
  
      }
  }
  </code></pre>