title: Java Double Linked List
body: <p>Alright, my professor (Data Structures class) assigned this: Your task is to write a program that can update character access frequencies in a doubly-Link list. The program should read one character at a time from a text file that contain many characters. To make it easier, do not count spaces. Every time a character is accessed, increment its access frequency by one in the node of the list. If the frequency of the current node is higher than of its previous node, the two nodes need to be swapped in the list. Continue doing so for all the previous nodes until no more previous node has lower access frequency. Eventually, the character with the highest frequency will appear at the beginning of the list, the next highest will be in the next node, etc. Your program also need to print out the characters in the list according to the order of the list. </p>
  
  <p>Here is the program I have made so far. It's just a doubly linked list as of right now. 
  My main question is how should I go about the "Every time a character is accessed, increment its access frequency by one in the node of the list. If the frequency of the current node is higher than of its previous node, the two nodes need to be swapped in the list."? </p>
  
  <p>I know there aren't any lines getting the info from a file. I'm going to add that later. 
  Any help is appreciated!</p>
  
  <pre><code>public class DoublyLinkedList {
  private class Node {
      String value;
      Node next,prev;
  
      public Node(String val, Node n, Node p) {
          value = val;
          next = n;
          prev=p;
      }
  
      Node(String val) {
          this(val, null, null);
      }
  }
  private Node first;
  private Node last;
  
  public DoublyLinkedList() {
      first = null;
      last = null;
  }
  public boolean isEmpty(){
      return first==null;
  }
  public int size(){
      int count=0;
      Node p=first;
      while(p!=null){
          count++;
          p=p.next;
      }
      return count;
  }
  public void add(String e) {
  
      if(isEmpty()){
          last=new Node(e);
          first=last;
      }
      else{
          last.next=new Node(e, null, last);
          last=last.next;
      }
  }
  public void add(int index, String e){
      if(index&lt;0||index&gt;size()){
          String message=String.valueOf(index);
          throw new IndexOutOfBoundsException(message);
      }
      if(index==0){
          Node p=first;
          first=new Node(e,p,null);
          if(p!=null)
              p.prev=first;
          if(last==null)
              last=first;
          return;
      }
      Node pred=first;
      for(int k=1; k&lt;=index-1;k++){
          pred=pred.next;
      }
      Node succ=pred.next;
      Node middle=new Node(e,succ,pred);
      pred.next=middle;
      if(succ==null)
          last=middle;
      else
          succ.prev=middle;
  }
  public String toString(){
      StringBuilder strBuilder=new StringBuilder();
      Node p=first;
      while(p!=null){
          strBuilder.append(p.value+"\n");
          p=p.next;
      }
      return strBuilder.toString();
  }
  public String remove(int index){
      if(index&lt;0||index&gt;=size()){
          String message=String.valueOf(index);
          throw new IndexOutOfBoundsException(message);
      }
      Node target=first;
      for(int k=1; k&lt;=index;k++){
          target=target.next;
      }
      String element=target.value;
      Node pred=target.prev;
      Node succ=target.next;
      if(pred==null)
          first=succ;
      else
          pred.next=succ;
      if(succ==null)
          last=pred;
      else
          succ.prev=pred;
      return element;
  }
  public boolean remove(String element){
      if(isEmpty())
          return false;
      Node target=first;
      while(target!=null&amp;&amp;!element.equals(target.value))
          target=target.next;
      if(target==null)
          return false;
      Node pred=target.prev;
      Node succ=target.next;
      if(pred==null)
          first=succ;
      else
          pred.next=succ;
      if(succ==null)
          last=pred;
      else
          succ.prev=pred;
      return true;
  }
  public static void main(String[] args){
      DoublyLinkedList list1=new DoublyLinkedList();
      String[] array={"a","c","e","f"};
      for(int i=0; i&lt;array.length; i++){
          list1.add(array[i]);
      }
      list1.add(1,"b");
      list1.add(3,"d");
      System.out.println(list1);
  
  }
  
  
  }
  </code></pre>