Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package linkedlist;
- public class SortedLinkedList {
- private Node first;
- private Node last;
- 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 == null || first.data.compareTo(e) == 0) {
- System.out.println("The element already exists in the list");
- } else {
- newNode = first;
- while (newNode.next != null && newNode.next.data.compareTo(e) != 0) {
- newNode = newNode.next;
- }
- }
- }
- /**
- * Prints the elements from the list in sorted order
- */
- public void udskrivElements() {
- Node temp = first;
- while (temp != null) {
- System.out.println(temp.data);
- temp = temp.next;
- }
- }
- /**
- * 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;
- }
- /**
- * Returns the number of elements in the list, by way of recursion
- */
- public int countElementsRecursive() {
- return countElementsRecursive(first);
- }
- public int countElementsRecursive(Node head) {
- if (head == null) {
- return 0;
- } else {
- return 1 + countElementsRecursive(head.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() {
- Node current = first;
- if (first == null) {
- return false;
- }
- if (current.next != null) {
- while (current.next != null) {
- if (current.next.next != null) {
- current.next = null;
- last = current;
- length--;
- return true;
- }
- current = current.next;
- }
- } else {
- first = null;
- last = null;
- length--;
- }
- return false;
- }
- /**
- * 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) {
- if (first != null) {
- if (first.data.equals(e)) {
- first = first.next;
- } else {
- Node temp = first;
- boolean found = false;
- while (!found && temp.next != null) {
- if (temp.next.data.equals(e)) {
- found = true;
- } else {
- temp = temp.next;
- }
- }
- if (found) {
- Node temp2 = temp.next;
- temp.next = temp2.next;
- temp2.next = null;
- }
- }
- }
- return false;
- }
- /**
- * 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