LoganBlackisle

singly linked list

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