Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linkedlist;
- import java.util.LinkedList;
- public class SortedLinkedList {
- LinkedList<String> list = new LinkedList<>();
- private Node first;
- int length;
- public SortedLinkedList() {
- first = new Node();
- }
- /**
- * Adds e to the list, so the list is still sorted by natural ordering of the
- * elements
- */
- public void addElement(String e) {
- Node newNode = new Node();
- if (first.data == null) {
- first.data = e;
- System.out.println("Success! " + e + " has been added!");
- } else if (first.data.compareTo(e) == 0) {
- System.out.println("The element already exists in the list");
- } else {
- while (first.next != null) {
- if (first.next.data.compareTo(e) != 0) {
- first.next.data = e;
- } else {
- first.next = first.next.next;
- }
- }
- }
- }
- /**
- * Prints the elements from the list in sorted order
- */
- public void printElements() {
- if (first != null) {
- Node temp = first;
- while (temp.next != null) {
- System.out.println(temp.data + " ");
- temp = temp.next;
- }
- System.out.println(temp.data + " ");
- }
- }
- /**
- * Returns the number of elements in the list
- */
- public int countElements() {
- int count = 0;
- Node temp = first;
- while (temp != null) {
- count++;
- temp = temp.next;
- }
- return count; //list.size();
- }
- /**
- * Returns the number of elements in the list, by way of recursion
- */
- public int countElementsRecursive() {
- return countElementsRecursive(first);
- }
- public int countElementsRecursive(Node node) {
- if (node == null) {
- return 0;
- } else {
- return 1 + countElementsRecursive(node.next);
- }
- }
- /**
- * Removes the last (the largest) element in the list. The list should still be
- * sorted by natural ordering of the elements
- *
- * @return true if an element was removed, else return false
- */
- public boolean removeLast() {
- if (first == null || first.next == null) {
- return false;
- }
- Node secondLast = first;
- while (secondLast.next.next != null) {
- secondLast = secondLast.next;
- }
- secondLast.next = null;
- return true;
- }
- /**
- * Removes the first occurrence of e in the list. The list should still be
- * sorted by natural ordering of the elements
- *
- * @return true if e was removed from the list, else return false
- */
- public boolean removeElement(String e) {
- Node temp = first;
- boolean removed = false;
- while (temp != null) {
- if (temp.data.equals(e)) {
- temp = null;
- removed = true;
- } else {
- temp = temp.next;
- }
- }
- return removed;
- }
- /**
- * Adds all the elements from list to the actual list. The list should still be
- * sorted by natural ordering of the elements
- */
- public void addAll(SortedLinkedList list) {
- Node temp = list.first;
- while (temp != null) {
- addElement(temp.data);
- temp = temp.next;
- }
- }
- private class Node {
- public Node() {
- }
- public String data;
- public Node next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement