Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A singly linked list.
- *
- * @author (NAME HERE PLZ)
- * @version (DATE HERE PLZ)
- */
- public class LinkedList<T> {
- private ListElement<T> first; // First element in list.
- private ListElement<T> last; // Last element in list.
- private int size; // Number of elements in list.
- /**
- * A list element.
- */
- private static class ListElement<T> {
- public T data;
- public ListElement<T> next;
- public ListElement(T data) {
- this.data = data;
- this.next = null;
- }
- }
- /**
- * Creates an empty list.
- */
- public LinkedList() {
- // TODO
- }
- /**
- * Inserts the given element at the beginning of this list.
- *
- * @param element An element to insert into the list.
- */
- public void addFirst(T element) {
- // TODO
- }
- /**
- * Inserts the given element at the end of this list.
- *
- * @param element An element to insert into the list.
- */
- public void addLast(T element) {
- // TODO
- }
- /**
- * @return The head of the list.
- * @throws NoSuchElementException if the list is empty.
- */
- public T getFirst() {
- // TODO
- return null;
- }
- /**
- * @return The tail of the list.
- * @throws NoSuchElementException if the list is empty.
- */
- public T getLast() {
- // TODO
- return null;
- }
- /**
- * Returns an element from a specified index.
- *
- * @param index A list index.
- * @return The element at the specified index.
- * @throws IndexOutOfBoundsException if the index is out of bounds.
- */
- public T get(int index) {
- // TODO
- return null;
- }
- /**
- * Removes the first element from the list.
- *
- * @return The removed element.
- * @throws NoSuchElementException if the list is empty.
- */
- public T removeFirst() {
- // TODO
- return null;
- }
- /**
- * Removes all of the elements from the list.
- */
- public void clear() {
- // TODO
- }
- /**
- * @return The number of elements in the list.
- */
- public int size() {
- // TODO
- return 0;
- }
- /**
- * Note that by definition, the list is empty if both first and last
- * are null, regardless of what value the size field holds (it should
- * be 0, otherwise something is wrong).
- *
- * @return <code>true</code> if this list contains no elements.
- */
- public boolean isEmpty() {
- return first == null && last == null;
- }
- /**
- * Creates a string representation of this list. The string
- * representation consists of a list of the elements enclosed in
- * square brackets ("[]"). Adjacent elements are separated by the
- * characters ", " (comma and space). Elements are converted to
- * strings by the method toString() inherited from Object.
- *
- * Examples:
- * "[1, 4, 2, 3, 44]"
- * "[]"
- *
- * @return A string representing the list.
- */
- public String toString() {
- // TODO
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement