Advertisement
Guest User

DoublyLinkedList

a guest
Dec 5th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. public class DoublyLinkedList<T> {
  2.  
  3.     // Do not add new instance variables or modify existing ones.
  4.     private Node<T> head;
  5.     private Node<T> tail;
  6.     private int size;
  7.  
  8.     // Do not add a constructor.
  9.  
  10.     /**
  11.      * Adds a node to the front of the list.
  12.      *
  13.      * Time Complexity: O(1).
  14.      *
  15.      * @param data  the data of the new node to add to the front of the list
  16.      * @throws      IllegalArgumentException if data is null
  17.      */
  18.     public void addToFront(T data) {
  19.  
  20.         if (data == null) {
  21.             throw new IllegalArgumentException("Cannot add null data");
  22.         }
  23.  
  24.         Node<T> newNode = new Node<>(data);
  25.         if (size == 0) {
  26.             head = tail = newNode;
  27.         }
  28.         else {
  29.             head.setPrevious(newNode);
  30.             newNode.setNext(head);
  31.             head = newNode;
  32.         }
  33.         size++;
  34.     }
  35.     /**
  36.      * Adds a node to the back of the list.
  37.      *
  38.      * Time Complexity: O(1).
  39.      *
  40.      * @param data  the data of the new node to add to the back of the list
  41.      * @throws      IllegalArgumentException if data is null
  42.      */
  43.     public void addToBack(T data) {
  44.         if (data == null) {
  45.             throw new IllegalArgumentException("Cannot add null data");
  46.         }
  47.  
  48.         Node<T> newNode = new Node<>(data);
  49.         if (size == 0) {
  50.             head = tail = newNode;
  51.         }
  52.         else {
  53.             tail.setNext(newNode);
  54.             newNode.setPrevious(tail);
  55.             tail = newNode;
  56.         }
  57.  
  58.         size++;
  59.     }
  60.  
  61.     /**
  62.      * Adds a node to the specified index.
  63.      *
  64.      * Time Complexity: O(n) in general, O(1) when index is 0 or size.
  65.      *
  66.      * @param index  the index at which to add the new node
  67.      * @param data   the data of the new node to add at the specified index
  68.      * @throws       IndexOutOfBoundsException if index < 0 or index > size
  69.      * @throws       IllegalArgumentException  if data is null
  70.      */
  71.     public void addAtIndex(int index, T data) {
  72.         if (index < 0 || index > size) {
  73.             String errMsg = String.format("Cannot add at index %d", index);
  74.             throw new IndexOutOfBoundsException(errMsg);
  75.         }
  76.  
  77.         if (data == null) {
  78.             throw new IllegalArgumentException("Cannot add null data");
  79.         }
  80.  
  81.         if (index == 0) {
  82.             addToFront(data);
  83.         }
  84.  
  85.         else if (index == size) {
  86.             addToBack(data);
  87.         }
  88.         else
  89.         {
  90.             Node<T> newNode = new Node<>(data);
  91.             Node<T>  node = head;
  92.             for (int i = 0; i < index-1; i++) {
  93.                 node = node.getNext();
  94.             }
  95.             newNode.setNext(node.getNext());
  96.             node.getNext().setPrevious(newNode);
  97.             node.setNext(newNode);
  98.             newNode.setPrevious(node);
  99.             size++;
  100.         }
  101.     }
  102.     /**
  103.      * Removes the first node of the list and returns its data.
  104.      *
  105.      * Time Complexity: O(1).
  106.      *
  107.      * @return  the data formerly located at the front of the list, null if the list is empty
  108.      */
  109.     public T removeFromFront() {
  110.         if(size == 0)
  111.             return null;
  112.         T data =head.getData();
  113.         if (size==1) {
  114.             head=tail=null;}
  115.         else {
  116.             head=head.getNext();
  117.             head.setPrevious(null); // line added
  118.         }
  119.         size--;
  120.         // Replace this line with your return statement
  121.         return data;
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Removes the last node of the list and returns its data.
  127.      *
  128.      * Time Complexity: O(1).
  129.      *
  130.      * @return  the data formerly located at the back of the list, null if the list is empty
  131.      */
  132.     public T removeFromBack() {
  133.         if(size == 0)
  134.             return null;
  135.         T data =tail.getData();
  136.  
  137.  
  138.         if (size==1) {
  139.             head=tail=null;}
  140.         else {
  141.  
  142.             tail=tail.getPrevious();
  143.             tail.setNext(null);
  144.         }
  145.         size--;
  146.         // Replace this line with your return statement
  147.         return data;
  148.     }
  149.  
  150.  
  151.     /**
  152.      * Removes the node at the specified index and returns its data.
  153.      *
  154.      * Time Complexity: O(n) in general, O(1) when index is 0 or size-1.
  155.      *
  156.      * @param index  the index of the node to remove
  157.      * @return       the data formerly located at the specified index
  158.      * @throws       IndexOutOfBoundsException if index < 0 or index >= size
  159.      */
  160.     public T removeAtIndex(int index) {
  161.         if (index < 0 || index >= size) {
  162.             String errMsg = String.format("Cannot add at index %d", index);
  163.             throw new IndexOutOfBoundsException(errMsg);
  164.         }
  165.  
  166.         if (index == 0) {
  167.             return removeFromFront();
  168.         }
  169.         else if (index==size-1) {          
  170.             return removeFromBack(); // line added
  171.         }
  172.         else
  173.         {
  174.             Node<T> node = head;
  175.  
  176.             for (int i = 0; i < index-1; i++) {
  177.                 node = node.getNext();
  178.             }
  179.             T data=node.getNext().getData();
  180.  
  181.             Node<T> temp = node.getNext().getNext();
  182.  
  183.             node.setNext(temp);
  184.             temp.setPrevious(node);
  185.  
  186.             size--;
  187.             // Replace this line with your return statement
  188.             return data;
  189.         }
  190.     }
  191.  
  192.  
  193.     /**
  194.      * Returns the data of the node at the specified index.
  195.      *
  196.      * Time Complexity: O(n) in general, O(1) when index is 0 or size-1.
  197.      *
  198.      * @param index  the index of the node to get data from
  199.      * @return       the data of the node at the specified index in the list
  200.      * @throws       IndexOutOfBoundsException if index < 0 or index >= size
  201.      */
  202.     public T get(int index) {
  203.         if (index < 0 || index >= size) {
  204.             String errMsg = String.format("Cannot get at index %d", index);
  205.             throw new IndexOutOfBoundsException(errMsg);
  206.         }
  207.  
  208.         if(index==0) {
  209.             return head.getData();
  210.         }
  211.         else if (index==size-1) {
  212.             return tail.getData();
  213.         }
  214.  
  215.         if(index < size/2) //Starting from the front
  216.         {
  217.             Node<T> nodeH = head.getNext();
  218.             for(int i = 1; i < index; i++) {
  219.                 nodeH = nodeH.getNext();
  220.             }
  221.             return nodeH.getData();
  222.         }
  223.         else //Starting from the back
  224.         {
  225.             Node<T> node = tail;
  226.             for(int i = size-1 ; i > index; i--) {
  227.                 node = node.getPrevious();
  228.             }
  229.             return node.getData(); 
  230.         }
  231.     }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.     /**
  238.      * Removes from the list the LAST NODE containing a copy of the given data.
  239.      *
  240.      * Time Complexity: O(n) in general, O(1) if data is in the tail.
  241.      *
  242.      * @param data  the data to be removed from the list
  243.      * @return      true if data is found in the list, false otherwise
  244.      * @throws      IllegalArgumentException if data is null
  245.      */
  246.     public boolean removeLastOccurrence(T data) {
  247.         if(size == 0)
  248.             throw new IllegalArgumentException("Cannot remove null data");
  249.         if(tail.getData().equals(data)) {
  250.             removeFromBack();
  251.             return true;
  252.         }
  253.         else if(head.getData().equals(data)) {
  254.             removeFromFront();
  255.             return true;
  256.         }
  257.         else if (size>1)
  258.         {
  259.             Node<T> temp = head;
  260.             for(int i =0;i<size-1;i++) {
  261.  
  262.                 if(temp.getData().equals(data)) {
  263.                     removeAtIndex(i);
  264.                     return true;
  265.                 }
  266.                 else
  267.                     temp=temp.getNext();   
  268.             }
  269.         }
  270.         // Replace this line with your return statement
  271.         return false;
  272.     }
  273.  
  274.  
  275.     /**
  276.      * Checks whether the list contains the given data.
  277.      *
  278.      * Time Complexity: O(n)
  279.      *
  280.      * @param data  the data to search for
  281.      * @return      true if data is found in the list, false otherwise
  282.      */
  283.     public boolean contains(T data) {
  284.         //if(data == null) return false;
  285.  
  286.         Node<T> node = head;
  287.         if(data.equals(tail.getData())) // when the data is on the tail
  288.             return true;
  289.  
  290.         while(node.getNext() != null ) {
  291.             if(data.equals(node.getData())) {
  292.                 return true;
  293.             }
  294.             else {
  295.                 node=node.getNext();
  296.             }
  297.         }
  298.         // Replace this line with your return statement
  299.         return false;
  300.     }
  301.  
  302.  
  303.     /**
  304.      * Clears all the data and resets the size of the list.
  305.      *
  306.      * Time Complexity: O(1).
  307.      */
  308.     public void clear() {
  309.         head=null;
  310.         tail=null;
  311.         size=0;
  312.     }
  313.     /**
  314.      * Returns the head node of the list.
  315.      *
  316.      * For grading purposes only. You shouldn't need this method since
  317.      * you have direct access to the variable.
  318.      *
  319.      * @return the node at the head of the list
  320.      */
  321.     public Node<T> getHead() {
  322.         // DO NOT MODIFY!
  323.         return head;
  324.     }
  325.  
  326.     /**
  327.      * Returns the tail node of the list.
  328.      *
  329.      * For grading purposes only. You shouldn't need this method since
  330.      * you have direct access to the variable.
  331.      *
  332.      * @return the node at the tail of the list
  333.      */
  334.     public Node<T> getTail() {
  335.         // DO NOT MODIFY!
  336.         return tail;
  337.     }
  338.  
  339.     /**
  340.      * Returns the size of the list.
  341.      *
  342.      * For grading purposes only. You shouldn't need this method since
  343.      * you have direct access to the variable.
  344.      *
  345.      * @return the size of the list
  346.      */
  347.     public int size() {
  348.         // DO NOT MODIFY!
  349.         return size;
  350.     }
  351.  
  352.     public void print() {
  353.         Node<T> node = head;
  354.         while (node != null) {
  355.             System.out.print(node.getData() + " ");
  356.             node = node.getNext();
  357.         }
  358.         System.out.println();
  359.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement