Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. class CDLList<T> {
  2.   class Node {
  3.     // Each Node object has these three fields
  4.     private T element;
  5.     private Node previous;
  6.     private Node next;
  7.  
  8.     // Constructor: Creates a Node object with element = e, previous = p and next = n
  9.     Node(T e, Node p, Node n) {
  10.       element = e;
  11.       previous = p;
  12.       next = n;
  13.     }
  14.  
  15.     // This function gets T e as input and sets e as the element of the Node
  16.     public void setElement(T e) {
  17.       element = e;
  18.     }
  19.  
  20.     // This function returns the element variable of the Node
  21.     public T getElement() {
  22.       return element;
  23.     }
  24.  
  25.     // This function gets Node n as input and sets the next variable of the current Node object as n.
  26.     public void setNext(Node n) {
  27.       next = n;
  28.     }
  29.  
  30.     // This function returns the next Node
  31.     public Node getNext() {
  32.       return next;
  33.     }
  34.  
  35.     // This function gets Node p as input and sets the previous variable of the current Node object as p.
  36.     public void setPrevious(Node p) {
  37.       previous = p;
  38.     }
  39.  
  40.     // This function returns the previous Node
  41.     public Node getPrevious() {
  42.       return previous;
  43.     }
  44.   }
  45.  
  46.   // Each object in CDLList has one field head, which points to the starting Node of CDLList.
  47.   private Node head;
  48.  
  49.   /**
  50.    * Constructor: initialises the head field as null
  51.    */
  52.   public CDLList() {
  53.     head = null;
  54.   }
  55.  
  56.   /**
  57.    * @return The element in the first Node of this CDLL. If the list is empty, this method returns null.
  58.    */
  59.   public T getFirst() {
  60.     // TODO
  61.     return (head != null) ? head.getElement() : null;
  62.   }
  63.  
  64.   /**
  65.    * Adds element e in a new Node to the head of the list.
  66.    *
  67.    * @param e
  68.    *     The element to add.
  69.    */
  70.   public void addFirst(T e) {
  71.     // TODO
  72.     if(head == null) {
  73.       head = new Node(e, null, null);
  74.     } else if(head.getNext() == null) {
  75.      
  76.       Node newNode = new Node(e, head, head);      
  77.       head.setPrevious(newNode);
  78.       head.setNext(newNode);
  79.       head = newNode;
  80.      
  81.     } else {
  82.      
  83.       Node newNode = new Node(e, head.getPrevious(), head);
  84.       Node tail = head.getPrevious();
  85.       tail.setNext(newNode);
  86.       head.setPrevious(newNode);
  87.       head = newNode;
  88.     }
  89.   }
  90.  
  91.   /**
  92.    * Remove the first Node in the list and return its element.
  93.    *
  94.    * @return The element of the first Node. If the list is empty, this method returns null.
  95.    */
  96.   public T removeFirst() {
  97.     // TODO
  98.     T answer;
  99.    
  100.     if(head == null) return null;
  101.     if(head.getNext() == null) {
  102.       answer = head.getElement();  
  103.       head = null;
  104.       return answer;
  105.     }
  106.    
  107.     answer = head.getElement();
  108.     Node tail = head.getPrevious();
  109.     tail.setNext(head.getNext());
  110.     head = tail.getNext();
  111.     head.setPrevious(tail);
  112.     return answer;
  113.   }
  114.  
  115.   /**
  116.    * @return The element in the last Node of the CDLL. If the list is empty, this method returns null.
  117.    */
  118.   public T getLast() {
  119.     // TODO
  120.     return (head == null) ? null : (head.getNext() == null ) ? head.getElement() : head.getPrevious().getElement();
  121.   }
  122.  
  123.   /**
  124.    * Adds element e in a new Node to the end of the list.
  125.    *
  126.    * @param e
  127.    *     The element to add.
  128.    */
  129.   public void addLast(T e) {
  130.     // TODO
  131.     if( head == null ) this.addFirst(e);
  132.     else if(head.getNext() == null ) {
  133.       Node newNode = new Node(e, head, head);
  134.       head.setNext(newNode);
  135.       head.setPrevious(newNode);
  136.     }
  137.     else {
  138.       Node tail = head.getPrevious();
  139.       Node newNode = new Node(e, tail, head);
  140.       tail.setNext(newNode);
  141.       head.setPrevious(newNode);
  142.       tail = newNode;
  143.     }
  144.   }
  145.  
  146.   /**
  147.    * Remove the last Node in the list and return its element.
  148.    *
  149.    * @return The element of the last Node. If the list is empty, this method returns null.
  150.    */
  151.   public T removeLast() {
  152.     // TODO
  153.     if(head == null || head.getNext() == null) return this.removeFirst();
  154.     else {
  155.       Node tail = head.getPrevious();
  156.       T answer = tail.getElement();
  157.       head.setPrevious(tail.getPrevious());
  158.       tail = tail.getPrevious();
  159.       tail.setNext(head);
  160.       return answer;
  161.     }
  162.   }
  163.  
  164.   /**
  165.    * Moves the head reference to the next element.
  166.    */
  167.   public void rotateForward() {
  168.     // TODO
  169.     if(head == null || head.getNext() == null) return;
  170.     else head = head.getNext();
  171.    
  172.   }
  173.  
  174.   /**
  175.    * Moves the head reference to the previous element.
  176.    */
  177.   public void rotateBackward() {
  178.     // TODO
  179.     if(head == null || head.getNext() == null) return;
  180.     else head = head.getPrevious();
  181.    
  182.   }
  183.  
  184.  
  185.   public void show(String string, int n) {
  186.     Node current = head;
  187.     System.out.println(string);
  188.     for(int i = 0; i < n; i++) {
  189.       System.out.print(current.getElement() + ", ");
  190.       current = current.getNext();
  191.     }
  192.   }
  193.  
  194. }
  195. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement