Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. /**
  2.  * A singly linked list.
  3.  *
  4.  * @author (NAME HERE PLZ)
  5.  * @version (DATE HERE PLZ)
  6.  */
  7. public class LinkedList<T> {
  8.     private ListElement<T> first;   // First element in list.
  9.     private ListElement<T> last;    // Last element in list.
  10.     private int size;               // Number of elements in list.
  11.    
  12.     /**
  13.      * A list element.
  14.      */
  15.     private static class ListElement<T> {
  16.         public T data;
  17.         public ListElement<T> next;
  18.        
  19.         public ListElement(T data) {
  20.             this.data = data;
  21.             this.next = null;
  22.         }
  23.     }
  24.    
  25.     /**
  26.      * Creates an empty list.
  27.      */
  28.     public LinkedList() {
  29.         // TODO
  30.     }
  31.  
  32.     /**
  33.      * Inserts the given element at the beginning of this list.
  34.      *
  35.      * @param element An element to insert into the list.
  36.      */
  37.     public void addFirst(T element) {
  38.         // TODO
  39.     }
  40.  
  41.     /**
  42.      * Inserts the given element at the end of this list.
  43.      *
  44.      * @param element An element to insert into the list.
  45.      */
  46.     public void addLast(T element) {
  47.         // TODO
  48.     }
  49.  
  50.     /**
  51.      * @return The head of the list.
  52.      * @throws NoSuchElementException if the list is empty.
  53.      */
  54.     public T getFirst() {
  55.         // TODO
  56.         return null;
  57.     }
  58.  
  59.     /**
  60.      * @return The tail of the list.
  61.      * @throws NoSuchElementException if the list is empty.
  62.      */
  63.     public T getLast() {
  64.         // TODO
  65.         return null;
  66.     }
  67.  
  68.     /**
  69.      * Returns an element from a specified index.
  70.      *
  71.      * @param index A list index.
  72.      * @return The element at the specified index.
  73.      * @throws IndexOutOfBoundsException if the index is out of bounds.
  74.      */
  75.     public T get(int index) {
  76.         // TODO
  77.         return null;
  78.     }
  79.  
  80.     /**
  81.      * Removes the first element from the list.
  82.      *
  83.      * @return The removed element.
  84.      * @throws NoSuchElementException if the list is empty.
  85.      */
  86.     public T removeFirst() {
  87.         // TODO
  88.         return null;
  89.     }
  90.  
  91.     /**
  92.      * Removes all of the elements from the list.
  93.      */
  94.     public void clear() {
  95.         // TODO
  96.     }
  97.  
  98.     /**
  99.      * @return The number of elements in the list.
  100.      */
  101.     public int size() {
  102.         // TODO
  103.         return 0;
  104.     }
  105.  
  106.     /**
  107.      * Note that by definition, the list is empty if both first and last
  108.      * are null, regardless of what value the size field holds (it should
  109.      * be 0, otherwise something is wrong).
  110.      *
  111.      * @return <code>true</code> if this list contains no elements.
  112.      */
  113.     public boolean isEmpty() {
  114.         return first == null && last == null;
  115.     }
  116.  
  117.     /**
  118.      * Creates a string representation of this list. The string
  119.      * representation consists of a list of the elements enclosed in
  120.      * square brackets ("[]"). Adjacent elements are separated by the
  121.      * characters ", " (comma and space). Elements are converted to
  122.      * strings by the method toString() inherited from Object.
  123.      *
  124.      * Examples:
  125.      *  "[1, 4, 2, 3, 44]"
  126.      *  "[]"
  127.      *
  128.      * @return A string representing the list.
  129.      */
  130.     public String toString() {
  131.         // TODO
  132.         return null;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement