Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. /**
  2.  * Defines the a singly-linked list class
  3.  * @author Ryan Riley Puzon
  4.  * @author Owehn Lagman
  5.  */
  6.  
  7. import java.util.NoSuchElementException;
  8.  
  9. public class List {
  10.     private class Node {
  11.         private int data;
  12.         private Node next;
  13.        
  14.         public Node(int data) {
  15.             this.data = data;
  16.             this.next = null;
  17.         }
  18.     }
  19.    
  20.     private int length;
  21.     private Node first;
  22.     private Node last;
  23.    
  24.   /****CONSTRUCTOR****/
  25.    
  26.     /**
  27.      * Instantiates a new List with default values
  28.      * @postcondition creates a new empty list with length of 0
  29.      */
  30.     public List() {
  31.         length = 0;
  32.         first = null;
  33.         last = null;
  34.        
  35.     }
  36.    
  37.     /****ACCESSORS****/
  38.    
  39.     /**
  40.      * Returns the value stored in the first node
  41.      * @precondition length != 0
  42.      * @return the integer value stored at node first
  43.      * @throws NoSuchElementException when precondition is violated
  44.      */
  45.     public int getFirst() throws NoSuchElementException{
  46.         if (length == 0) {
  47.             throw new NoSuchElementException("getFirst: List is Empty. No data to access!");
  48.         }
  49.         return first.data;
  50.     }
  51.    
  52.     /**
  53.      * Returns the value stored in the last node
  54.      * @precondition length != 0
  55.      * @return the integer value stored in the node last
  56.      * @throws NoSuchElementException when precondition is violated
  57.      */
  58.     public int getLast() throws  NoSuchElementException{
  59.         if(length == 0) {
  60.             throw new NoSuchElementException("getLast: List is Empty. No data to access!");
  61.         }
  62.         return last.data;
  63.     }
  64.     public int testfunction() {
  65.         getLast();
  66.         return 0;
  67.     }
  68.     /**
  69.      * Returns the current length of the list
  70.      * @return the length of the list from 0 to n
  71.      */
  72.     public int getLength() {
  73.         return length;
  74.     }
  75.    
  76.     /**
  77.      * Returns whether the list is currently empty
  78.      * @return whether the list is empty
  79.      */
  80.     public boolean isEmpty() {
  81.        return length == 0;
  82.     }
  83.    
  84.     /****MUTATORS****/
  85.    
  86.     /**
  87.      * Creates a new first element
  88.      * @param data the data to insert at the
  89.      * front of the list
  90.      * @postcondition inserts a new given data point at the front of the list and creates new first
  91.      */
  92.     public void addFirst(int data) {
  93.         if (first == null) {
  94.             first = last = new Node(data);
  95.         } else {
  96.             Node N = new Node(data);
  97.             N.next = first;
  98.             first = N;
  99.         }
  100.         length++;
  101.     }
  102.    
  103.     /**
  104.      * Creates a new last element
  105.      * @param data the data to insert at the
  106.      * end of the list
  107.      * @postcondition inserts a new given data at the end of the list and creates new last
  108.      */
  109.     public void addLast(int data) {
  110.         if(length==0) {
  111.             first = last = new Node(data);
  112.         } else {
  113.             Node temp = new Node(data);
  114.             last.next = temp;
  115.             last = temp;
  116.         }
  117.         length++;
  118.         return;
  119.     }
  120.    
  121.     /**
  122.     * removes the element at the front of the list
  123.     * @precondition length != 0
  124.     * @postcondition removes the first element and makes the next element in the list the new first
  125.     * @throws NoSuchElementException when precondition is violated
  126.     */
  127.     public void removeFirst() throws NoSuchElementException{
  128.         if(length == 0) {
  129.             throw new
  130.             NoSuchElementException("removeFirst(): Cannot remove from an empty List!");
  131.         } else if (length == 1) {
  132.             first = last = null;
  133.         } else {
  134.         first = first.next;
  135.         }
  136.         length--;
  137.         return;
  138.     }
  139.    
  140.     /**
  141.      * removes the element at the end of the list
  142.      * @precondition length != 0
  143.      * @postcondition removes the last node and makes the prior element before the last the new last element of the list
  144.      * @throws NoSuchElementException when precondition is violated
  145.      */
  146.       public void removeLast() throws NoSuchElementException {
  147.           //add your if statement here
  148.           if (length == 0) {
  149.               throw new NoSuchElementException("removeLast(): Cannot remove from an empty List!");
  150.           }
  151.           else if (length == 1) {
  152.               //add a line of code here
  153.               first = last = null;
  154.           } else {
  155.               Node temp = first;
  156.               while (temp.next != last) {
  157.                   temp = temp.next;
  158.               }
  159.               last = temp;
  160.               last.next = null;
  161.           }
  162.           length--;
  163.           return;
  164.       }
  165.    
  166.     /****ADDITIONAL OPERATIONS****/
  167.    
  168.       /**
  169.        * List with each value separated by a blank space
  170.        * At the end of the List a new line
  171.        * @return the List as a String for display
  172.        */
  173.         @Override public String toString() {
  174.             String result = "";
  175.             Node temp = first;
  176.             while(temp != null) {
  177.                 result += temp.data + " ";
  178.                 temp = temp.next;
  179.                 //fill in here
  180.             }
  181.             result += "\n";
  182.             return result;
  183.         }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement