Java Data Structures Doubly Linked List

Description

Java Data Structures Doubly Linked List


class Link {/*from   w  w w  . j a v a 2 s . c  om*/
   public long dData; // data item
   public Link next; // next link in list
   public Link previous; // previous link in list

   public Link(long d) {
      dData = d;
   }

   public void displayLink() {
      System.out.print(dData + " ");
   }
}

class DoublyLinkedList {
   private Link first; // ref to first item
   private Link last; // ref to last item

   public DoublyLinkedList() {
      first = null;
      last = null;
   }

   public boolean isEmpty() {
      return first == null;
   }

   public void insertFirst(long dd) {
      Link newLink = new Link(dd); // make new link
      if (isEmpty())
         last = newLink;
      else
         first.previous = newLink;
      newLink.next = first;
      first = newLink;
   }

   public void insertLast(long dd) {
      Link newLink = new Link(dd);
      if (isEmpty())
         first = newLink;
      else {
         last.next = newLink;
         newLink.previous = last;
      }
      last = newLink;
   }

   public Link deleteFirst() {
      Link temp = first;
      if (first.next == null)
         last = null;
      else
         first.next.previous = null;
      first = first.next;
      return temp;
   }

   public Link deleteLast() {
      Link temp = last;
      if (first.next == null)
         first = null;
      else
         last.previous.next = null;
      last = last.previous;
      return temp;
   }

   public boolean insertAfter(long key, long dd) {
      Link current = first;
      while (current.dData != key) {
         current = current.next;
         if (current == null)
            return false;
      }
      Link newLink = new Link(dd);

      if (current == last) {
         newLink.next = null;
         last = newLink;
      } else {
         newLink.next = current.next;

         current.next.previous = newLink;
      }
      newLink.previous = current;
      current.next = newLink;
      return true;
   }

   public Link deleteKey(long key) {
      Link current = first;
      while (current.dData != key) {
         current = current.next;
         if (current == null)
            return null;
      }
      if (current == first)
         first = current.next;
      else
         current.previous.next = current.next;

      if (current == last)
         last = current.previous;
      else
         current.next.previous = current.previous;
      return current; // return value
   }

   public void displayForward() {
      System.out.print("List (first-->last): ");
      Link current = first;
      while (current != null) {
         current.displayLink();
         current = current.next;
      }
      System.out.println("");
   }

   public void displayBackward() {
      System.out.print("List (last-->first): ");
      Link current = last;
      while (current != null) {
         current.displayLink();
         current = current.previous;
      }
      System.out.println("");
   }

}

public class Main {
   public static void main(String[] args) {
      DoublyLinkedList theList = new DoublyLinkedList();

      theList.insertFirst(2);
      theList.insertFirst(4);
      theList.insertFirst(6);

      theList.insertLast(1);
      theList.insertLast(3);
      theList.insertLast(5);

      theList.displayForward(); // display list forward
      theList.displayBackward(); // display list backward

      theList.deleteFirst(); // delete first item
      theList.deleteLast(); // delete last item
      theList.deleteKey(11); // delete item with key 11

      theList.displayForward(); // display list forward

      theList.insertAfter(2, 77); 
      theList.insertAfter(3, 88); 

      theList.displayForward(); // display list forward
   }
}



PreviousNext

Related