SHARE
TWEET

Untitled

a guest Jun 19th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DList<E> {
  2.    
  3.     //declares our variables
  4.     DListNode<E> head;
  5.     DListNode<E> tail;
  6.     private int length;
  7.    
  8.     public DList()
  9.     {
  10.         this.length = 0;  //initializes length to zero.
  11.         this.head = null; //initializes head to null.
  12.         this.tail = null; //initializes tail to null.
  13.     }
  14.    
  15.     public void addAtHead(E element) //method which adds an element to the Head node.
  16.     {
  17.     DListNode<E> node = new DListNode<E>(element, null, null);
  18.     this.addAtHead(node);  //creates a new node at the head.
  19.  
  20.     }
  21.    
  22.     public void addAtTail(E element)  //method which adds an element to the tail node
  23.     {  
  24.     DListNode<E> node = new DListNode<E>(element, null, null);
  25.     this.addAtTail(node);  //creates a new node at the tail
  26.  
  27.     }
  28.    
  29.     public void addAtHead(DListNode<E> node)  //method which adds a new node at the head
  30.     {
  31.    
  32.         if(head == null){   // if the value of head is null, sets the head and tail equal to node
  33.             head = node;
  34.             tail = node;
  35.         }
  36.         else if(head != null) {
  37.             head.setPredecessor(node);   // if head isn't null sets the new node as the head nodes predecessor.
  38.             node.setSuccessor(head);     //sets the new node as the current heads successor.
  39.             head = node;                 //sets the new node equal to the head since it becomes the new head.
  40.         }
  41.         this.length++;                   //increments the size of the list.
  42.        
  43.     }
  44.    
  45.     public void addAtTail(DListNode<E> node)  //method which adds a new node at the tail
  46.     {
  47.  
  48.        
  49.         if(tail == null){    // if the value of tail is null, sets the head and tail equal to new node
  50.             head = node;
  51.             tail = node;
  52.         }
  53.         else if(tail != null){
  54.        
  55.             tail.setSuccessor(node);   //if the tail isn't null, sets the new node as the current tails successor.
  56.             node.setPredecessor(head);  //sets the new node as the current heads predecessor
  57.             tail = node;                 // sets the tail equal to the new node
  58.         }
  59.         this.length++;  // increments the length
  60.     }
  61.    
  62.     public void removeFromHead()  //method which removes node from head.
  63.     {
  64.        
  65.         if (length == 1) {   //if the length is 1, the tail and head are both null.
  66.             head = null;
  67.             tail = null;
  68.         }
  69.         else if(length >1)  
  70.         {
  71.             DListNode<E> remove = head.getSuccessor();  //declares a new variable remove set equal to the heads successor.
  72.             remove.setPredecessor(null);                  //sets removes predecessor to null.
  73.             head = remove;  //sets remove and head equal to each other.
  74.         }
  75.        
  76.         length--;  //decrements the length
  77.  
  78.        
  79.     }
  80.    
  81.     public void removeFromTail()
  82.     {
  83.         if(length == 1){    //if the length is 1, the tail and head are both null.
  84.             head = null;
  85.             tail = null;
  86.         }
  87.         else if(length > 1){
  88.             DListNode<E> remove = tail.getPredecessor();    //declares a new variable remove set equal to the tails predecessor.
  89.             remove.setSuccessor(null);                      //sets the variable removes successor to null.
  90.             tail = remove;                                  //sets tail and remove equal to another.
  91.            
  92.         }
  93.         this.length--;  //decrements the length
  94.     }
  95.    
  96.     public DListNode<E> getHead()  //method which returns the head
  97.     {
  98.         return this.head;
  99.        
  100.     }
  101.    
  102.     public void setHead(DListNode<E> head)   //method which sets the head to value head
  103.     {
  104.         this.head = head;
  105.     }
  106.    
  107.     public int getLength()  //method which gets the length
  108.     {
  109.         return this.length;
  110.     }
  111.    
  112.     public void setLength(int length) //method which sets the length to value length
  113.     {
  114.        
  115.         this.length = length;
  116.     }
  117.    
  118.     public DListNode<E> getTail() //method which gets the tail
  119.     {
  120.         return this.tail;
  121.     }
  122.    
  123.     public void setTail(DListNode<E> tail)  //method which sets the tail to value tail.
  124.     {
  125.         this.tail = tail;
  126.     }
  127.  
  128. }
RAW Paste Data
Top