/** * A list implemented by a linked structure. * * @author Franck van Breugel */ public class LinkedList implements List { private Node head; private int size; // redundant (space versus time trade off) public LinkedList() { this.setHead(new Node(null, null)); // dummy node this.setSize(0); } /** * Returns the head of this list. * * @return the head of this list. */ private Node getHead() { return this.head; } /** * Sets the head of this list to the given node. * * @param head the head of this list. */ private void setHead(Node head) { this.head = head; } /** * Returns the size of this list. * * @return the size of this list. */ private int getSize() { return this.size; } /** * Sets the size of this list to the given size. * * @param size the size of this list. */ private void setSize(int size) { this.size = size; } public boolean add(String element) { this.setSize(this.getSize() + 1); return this.add(element, this.getHead()); } /** * Appends the specified element to the end of the sublist of this list * starting at the given node. * * @param element a string. * @pre. element != null * @param node first node of the sublist. * @return true */ private boolean add(String element, Node node) { if (node.getNext() == null) { node.setNext(new Node(element, null)); return true; } else { return this.add(element, node.getNext()); } } public String get(int index) { return this.get(index, this.head.getNext()); } /** * Returns the element at the given position in the sublist of this list * starting at the given node. * * @param index index of the element to return. * @pre. index >= 0 && index < size of the sublist * @param node first node of the sublist. * @return the element at the given position in this list. */ private String get(int index, Node node) { if (index == 0) { return node.getElement(); } else { return this.get(index - 1, node.getNext()); } } public String set(int index, String element) { return this.set(index, element, this.head.getNext()); } /** * Replaces the element at the given position in the sublist of this * list starting at the given node with the given element. Returns * the element previously at the specified position. * * @param index index of the element to replace. * @pre. index >= 0 && index < size() * @param element element to be stored at the given position. * @pre. element != null * @param node first node of the sublist. * @return the element previously at the specified position. */ private String set(int index, String element, Node node) { if (index == 0) { String oldElement = node.getElement(); node.setElement(element); return oldElement; } else { return this.set(index - 1, element, node.getNext()); } } public int size() { return this.size; //return this.size(this.head); } /* private int size(Node node) { if (node.getNext() == null) { return 0; } else { return this.size(node.getNext()) + 1; } } */ public String toString() { return "[" + this.toString(this.head.getNext()) + "]"; } /** * Returns a string representation of the sublist of this list * starting at the given node. The string representation consists * of a list of the elements in the order they are returned by * its iterator. Adjacent elements are separated by the characters * ", " (comma and space). * * @return a string representation of this collection. */ private String toString(Node node) { if (node == null) { return ""; } else if (node.getNext() == null) { return node.getElement(); } else { return node.getElement() + ", " + this.toString(node.getNext()); } } public boolean contains(String element) { return this.contains(element, this.getHead().getNext()); } /** * Tests if the sublist of this list starting at the given node * contains the given element. * * @param element a string. * @pre. element != null * @param node first node of the sublist. * @return true if the sublist of this list starting at the given * node contains the given element, false otherwise. */ private boolean contains(String element, Node node) { if (node == null) { return false; } else if (node.getElement().equals(element)) { return true; } else { return this.contains(element, node.getNext()); } } }