Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. class Solution {
  2.       public static class Node
  3.       {
  4.         // Each node object has these three variables
  5.         private Object element;
  6.         private Node   next;
  7.         private Node   previous;
  8.        
  9.         // Constructor: Creates a Node object with element = e, next = n and previous = p
  10.         Node(Object e, Node n, Node p)
  11.         {
  12.           element = e;
  13.           next    = n;
  14.           previous = p;
  15.         }
  16.      
  17.         // This function gets Object e as input and sets e as the element of the Node
  18.         public void setElement(Object e)
  19.         {
  20.           element = e;
  21.         }
  22.        
  23.         // This function returns the element variable of the Node
  24.         public Object getElement()
  25.         {
  26.           return element;
  27.         }
  28.        
  29.         // This function gets Node n as input and sets the next variable of the current Node object as n.
  30.         public void setNext(Node n)
  31.         {
  32.           next = n;
  33.         }
  34.        
  35.         // This function returns the next Node
  36.         public Node getNext()
  37.         {
  38.           return next;
  39.         }
  40.        
  41.         // This function gets Node n as input and sets the previous variable of the current Node object as p.
  42.         public void setPrevious(Node p)
  43.         {
  44.           previous = p;
  45.         }
  46.        
  47.         // This function returns the previous Node
  48.         public Node getPrevious()
  49.         {
  50.           return previous;
  51.         }
  52.        
  53.       }
  54.      
  55.       public static class DLList
  56.       {
  57.      
  58.         // Each object in DLList has one variable head, which points to the starting Node of DLList.
  59.         private Node head;
  60.         private Node tail;
  61.        
  62.         // Implemens an empty constructor which initialises the head variable as null
  63.         DLList()
  64.         {
  65.           head = null;
  66.           tail = null;
  67.         }
  68.  
  69.         // Returns the head node of the DLL
  70.         public Node getHead()
  71.         {
  72.           return head;
  73.         }
  74.        
  75.        
  76.  
  77.         // Returns the tail node of the DLL
  78.         public Node getTail()
  79.         {
  80.             return tail;
  81.         }
  82.        
  83.         // Following methods are the ones you are expected to implement
  84.        
  85.         // Adds node n to the head of the list.
  86.         public void addFirst(Node n)
  87.         {
  88.           if(n != null) {
  89.             Node otherNode = head;
  90.             head = n;
  91.             if (otherNode == null) tail = n;
  92.             else {
  93.               otherNode.setPrevious(n);
  94.               n.setNext(otherNode);
  95.           }
  96.           }
  97.         }
  98.        
  99.         //Removes and returns the first node in the list. If the list is empty, returns null.
  100.         public Node removeFirst()
  101.         {
  102.           Node otherNode;
  103.           if (tail == null && head == null) otherNode = null;
  104.           else {
  105.             otherNode = head;
  106.             head.getNext().setPrevious(null);
  107.             head = head.getNext();
  108.           }
  109.           return otherNode;
  110.         }
  111.        
  112.         // Adds node n to the end of the list
  113.         public void addLast(Node n)
  114.         {
  115.           if (n != null) {
  116.           Node otherNode = tail;
  117.             tail = n;
  118.             if (otherNode == null) head = n;
  119.             else {
  120.               otherNode.setNext(n);
  121.               n.setPrevious(otherNode);
  122.             }
  123.           }
  124.         }
  125.          
  126.         //Removes and returns the last node in the list.If the list is empty, returns null.
  127.         public Node removeLast() {
  128.           Node otherNode;
  129.           if (tail == null && head == null) otherNode = null;
  130.           else {
  131.             otherNode = tail;
  132.             tail.getPrevious().setNext(null);
  133.             tail = tail.getPrevious();
  134.           }
  135.           return otherNode;
  136.         }
  137.        
  138.         // Returns the number of nodes in the list
  139.         public int size()
  140.         {
  141.           int size = 0;
  142.           if (head == null && tail == null) return size;
  143.           else {
  144.             for (Node temp = head; temp != null; temp = temp.getNext()) {
  145.               size++;
  146.             }
  147.           }
  148.           return size;
  149.         }
  150.      
  151.         // Adds node n after the node in position pos. The list is zero indexed, so the first element in the list correspond
  152.         // to position 0. If there is no node in position pos, this method adds it to the last position.
  153.         public void addAtPosition(Node n, int pos)
  154.         {
  155.           if (n != null) {
  156.           if (pos < size() - 1) {
  157.               if (pos == -1) {
  158.                 addFirst(n);
  159.               } else {
  160.                 n.setNext(null);
  161.                 Node current = head;
  162.                 Node previous = null;
  163.                 for (int i = 0; i <= pos; i++) {
  164.                   previous = current;
  165.                   current = current.getNext();
  166.                 }
  167.            
  168.                 n.setNext(previous.getNext());
  169.                 previous.setNext(n);
  170.             }
  171.             } else {
  172.                addLast(n);
  173.             }
  174.           }
  175.         }
  176.        
  177.         // Remove and return node n at position pos. The list is zero indexed, so the first element in the list correspond
  178.         // to position 0. If there is no node in position pos, this method returns null.
  179.         public Node removeFromPosition(int pos)
  180.         {
  181.           if (head == null && tail == null) {
  182.               return null;
  183.           }
  184.           if (pos < size()) {
  185.             if (pos == 0) {
  186.               return removeFirst();
  187.             } else if (pos == size()-1) {
  188.               return removeLast();
  189.             } else {
  190.                 Node temp = head;
  191.               for (int i = 0; i < pos-1; i++) {
  192.                   temp = temp.getNext();
  193.               }
  194.               Node next = temp.getNext().getNext();
  195.               Node result = temp.getNext();
  196.               temp.setNext(next);
  197.               return result;
  198.            }
  199.           }
  200.           return null;
  201.         }
  202.        
  203.         // Returns  a new DLL that contains the elements of the current one in reversed order
  204.         public DLList reverse()
  205.         {
  206.           DLList list = new DLList();
  207.          
  208.           if (head != null && tail != null) {
  209.             Node temp = tail;
  210.             while (temp != null) {
  211.               Node newNode = new Node(temp.getElement(), null, null);
  212.               list.addLast(newNode);
  213.               temp = temp.getPrevious();
  214.             }
  215.           }
  216.           return list;
  217.         }
  218.       }
  219. }//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement