Advertisement
Guest User

csll

a guest
Jan 21st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. class CSLList<T> {
  2.   class Node {
  3.     // Each Node object has these two fields
  4.     private T element;
  5.     private Node next;
  6.  
  7.     // Constructor: Creates a Node object with element = e and next = n
  8.     Node(T e, Node n) {
  9.       element = e;
  10.       next = n;
  11.     }
  12.  
  13.     // This function gets T e as input and sets e as the element of the Node
  14.     public void setElement(T e) {
  15.       element = e;
  16.     }
  17.  
  18.     // This function returns the element variable of the Node
  19.     public T getElement() {
  20.       return element;
  21.     }
  22.  
  23.     // This function gets Node n as input and sets the next variable of the current Node object as n.
  24.     public void setNext(Node n) {
  25.       next = n;
  26.     }
  27.  
  28.     // This function returns the next Node
  29.     public Node getNext() {
  30.       return next;
  31.     }
  32.   }
  33.  
  34.   // Each object in CSLList has one field tail, which points to the tail Node of CSLList.
  35.   private Node tail;
  36.  
  37.   /**
  38.     * Constructor: initialises the tail field as null
  39.     */
  40.   public CSLList() {
  41.     tail = null;
  42.   }
  43.  
  44.   /**
  45.     * @return The element in the first Node of this CSLL. If the list is empty, this method returns null.
  46.     */
  47.   public T getFirst() {
  48.     if (tail == null)
  49.       return null;
  50.     return tail.getNext().getElement();
  51.   }
  52.  
  53.   /**
  54.     * @return The element in the last Node of this CSLL. If the list is empty, this method returns null.
  55.     */
  56.   public T getLast() {
  57.     if (tail == null)
  58.       return null;
  59.     return tail.getElement();
  60.   }
  61.  
  62.   /**
  63.     * Adds element e in a new Node to the head of the list.
  64.     *
  65.     * @param e
  66.     *     The element to add.
  67.     */
  68.   public void addFirst(T e) {
  69.     if(tail == null) {
  70.       tail = new Node(e, null);
  71.       tail.setNext(tail);
  72.     }
  73.     else {
  74.       Node node = new Node(e, tail.getNext());
  75.       tail.setNext(node);
  76.     }
  77.   }
  78.  
  79.   /**
  80.     * Adds element e in a new Node to the tail of the list.
  81.     *
  82.     * @param e
  83.     *     The element to add.
  84.     */
  85.   public void addLast(T e) {
  86.       addFirst(e);
  87.       rotate();
  88.   }
  89.  
  90.   /**
  91.     * Remove the first Node in the list and return its element.
  92.     *
  93.     * @return The element of the first Node. If the list is empty, this method returns null.
  94.     */
  95.   public T removeFirst() {
  96.     if (tail != null) {
  97.       Node node = tail.getNext();
  98.       if(node == tail) {
  99.         tail = null;
  100.       }
  101.       else {
  102.         tail.setNext(node.getNext());
  103.       }
  104.       return node.getElement();
  105.     }
  106.     return null;
  107.   }
  108.  
  109.   /**
  110.     * Rotates the list such that the second element in the list will become the first element in the list.
  111.     * Example: rotating the list [1, 2, 3] will become [2, 3, 1].
  112.     */
  113.   public void rotate() {
  114.     if(tail != null) {
  115.       tail = tail.getNext();
  116.     }
  117.   }
  118.  
  119.   public int size() {
  120.         if (tail == null) {
  121.             return 0;
  122.         }
  123.         int size = 1;
  124.         Node currNode = tail;
  125.         while (currNode.getNext() != tail) {
  126.             size++;
  127.             currNode = currNode.getNext();
  128.         }
  129.         return size;
  130.     }
  131.  
  132.   /**
  133.     * Merges this list and the other list by alternating elements from the two lists.
  134.     * If one of the lists is longer than the other, the remaining elements are added to the end of the resulting list.
  135.     *
  136.     * @param other
  137.     *     The other list to alternate elements with. Is treated as an empty list if it is null.
  138.     * @return A new list with alternated elements of this list and the other list.
  139.     */
  140.   public CSLList<T> alternate(CSLList<T> other) {
  141.     if(other == null || other.tail == null) {
  142.       return this;
  143.     }
  144.     if (this.tail == null) {
  145.       return other;
  146.     }
  147.     CSLList<T> list = new CSLList<>();
  148.     int thisSize = this.size();
  149.     int otherSize = other.size();
  150.     int min = Math.min(thisSize, otherSize);
  151.    
  152.     Node nodeA = this.tail.getNext();
  153.     Node nodeB = other.tail.getNext();
  154.    
  155.     for(int i = 0; i < min; i++) {
  156.       list.addLast(nodeA.getElement());
  157.       list.addLast(nodeB.getElement());
  158.       nodeA = nodeA.getNext();
  159.       nodeB = nodeB.getNext();
  160.     }
  161.     if (min == thisSize) {
  162.       for(int i = 0; i < otherSize-min; i++) {
  163.         list.addLast(nodeB.getElement());
  164.         nodeB = nodeB.getNext();
  165.       }
  166.     }
  167.     else {
  168.       for(int i = 0; i < thisSize-min; i++) {
  169.         list.addLast(nodeA.getElement());
  170.         nodeA = nodeA.getNext();
  171.       }
  172.     }
  173.     return list;
  174.   }
  175.  
  176.   /**
  177.     * Removes all elements at the odd positions in this list.
  178.     * Note that the head of the list is element number 0, which is an even position.
  179.     */
  180.   public void dropOdd() {
  181.     if (tail == null || tail.getNext() == tail) {
  182.       return;
  183.     }
  184.     int size = size();
  185.     for(int i = 0; i < size; i++) {
  186.       T e = removeFirst();
  187.       if(i % 2 == 0) {
  188.         addLast(e);
  189.       }
  190.     }
  191.   }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement