Advertisement
LoganBlackisle

SortedLinkedList

Jul 31st, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package linkedlist;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public class SortedLinkedList {
  6.     LinkedList<String> list = new LinkedList<>();
  7.     private Node first;
  8.     int length;
  9.  
  10.     public SortedLinkedList() {
  11.         first = new Node();
  12.     }
  13.  
  14.     /**
  15.      * Adds e to the list, so the list is still sorted by natural ordering of the
  16.      * elements
  17.      */
  18.     public void addElement(String e) {
  19.         Node newNode = new Node();
  20.         if (first.data == null) {
  21.             first.data = e;
  22.             System.out.println("Success! " + e + " has been added!");
  23.         } else if (first.data.compareTo(e) == 0) {
  24.             System.out.println("The element already exists in the list");
  25.         } else {
  26.             while (first.next != null) {
  27.                 if (first.next.data.compareTo(e) != 0) {
  28.                     first.next.data = e;
  29.                 } else {
  30.                     first.next = first.next.next;
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     /**
  37.      * Prints the elements from the list in sorted order
  38.      */
  39.     public void printElements() {
  40.         if (first != null) {
  41.             Node temp = first;
  42.             while (temp.next != null) {
  43.                 System.out.println(temp.data + " ");
  44.                 temp = temp.next;
  45.             }
  46.             System.out.println(temp.data + " ");
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * Returns the number of elements in the list
  52.      */
  53.     public int countElements() {
  54.         int count = 0;
  55.         Node temp = first;
  56.         while (temp != null) {
  57.             count++;
  58.             temp = temp.next;
  59.         }
  60.         return count; //list.size();
  61.     }
  62.  
  63.     /**
  64.      * Returns the number of elements in the list, by way of recursion
  65.      */
  66.     public int countElementsRecursive() {
  67.         return countElementsRecursive(first);
  68.     }
  69.  
  70.     public int countElementsRecursive(Node node) {
  71.         if (node == null) {
  72.             return 0;
  73.         } else {
  74.             return 1 + countElementsRecursive(node.next);
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Removes the last (the largest) element in the list. The list should still be
  80.      * sorted by natural ordering of the elements
  81.      *
  82.      * @return true if an element was removed, else return false
  83.      */
  84.     public boolean removeLast() {
  85.         if (first == null || first.next == null) {
  86.             return false;
  87.         }
  88.         Node secondLast = first;
  89.         while (secondLast.next.next != null) {
  90.             secondLast = secondLast.next;
  91.         }
  92.         secondLast.next = null;
  93.         return true;
  94.     }
  95.  
  96.     /**
  97.      * Removes the first occurrence of e in the list. The list should still be
  98.      * sorted by natural ordering of the elements
  99.      *
  100.      * @return true if e was removed from the list, else return false
  101.      */
  102.     public boolean removeElement(String e) {
  103.         Node temp = first;
  104.         boolean removed = false;
  105.         while (temp != null) {
  106.             if (temp.data.equals(e)) {
  107.                 temp = null;
  108.                 removed = true;
  109.             } else {
  110.                 temp = temp.next;
  111.             }
  112.         }
  113.         return removed;
  114.     }
  115.  
  116.     /**
  117.      * Adds all the elements from list to the actual list. The list should still be
  118.      * sorted by natural ordering of the elements
  119.      */
  120.     public void addAll(SortedLinkedList list) {
  121.         Node temp = list.first;
  122.         while (temp != null) {
  123.             addElement(temp.data);
  124.             temp = temp.next;
  125.         }
  126.     }
  127.  
  128.     private class Node {
  129.         public Node() {
  130.         }
  131.  
  132.         public String data;
  133.         public Node next;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement