Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Defines the a singly-linked list class
- * @author Ryan Riley Puzon
- * @author Owehn Lagman
- */
- import java.util.NoSuchElementException;
- public class List {
- private class Node {
- private int data;
- private Node next;
- public Node(int data) {
- this.data = data;
- this.next = null;
- }
- }
- private int length;
- private Node first;
- private Node last;
- /****CONSTRUCTOR****/
- /**
- * Instantiates a new List with default values
- * @postcondition creates a new empty list with length of 0
- */
- public List() {
- length = 0;
- first = null;
- last = null;
- }
- /****ACCESSORS****/
- /**
- * Returns the value stored in the first node
- * @precondition length != 0
- * @return the integer value stored at node first
- * @throws NoSuchElementException when precondition is violated
- */
- public int getFirst() throws NoSuchElementException{
- if (length == 0) {
- throw new NoSuchElementException("getFirst: List is Empty. No data to access!");
- }
- return first.data;
- }
- /**
- * Returns the value stored in the last node
- * @precondition length != 0
- * @return the integer value stored in the node last
- * @throws NoSuchElementException when precondition is violated
- */
- public int getLast() throws NoSuchElementException{
- if(length == 0) {
- throw new NoSuchElementException("getLast: List is Empty. No data to access!");
- }
- return last.data;
- }
- public int testfunction() {
- getLast();
- return 0;
- }
- /**
- * Returns the current length of the list
- * @return the length of the list from 0 to n
- */
- public int getLength() {
- return length;
- }
- /**
- * Returns whether the list is currently empty
- * @return whether the list is empty
- */
- public boolean isEmpty() {
- return length == 0;
- }
- /****MUTATORS****/
- /**
- * Creates a new first element
- * @param data the data to insert at the
- * front of the list
- * @postcondition inserts a new given data point at the front of the list and creates new first
- */
- public void addFirst(int data) {
- if (first == null) {
- first = last = new Node(data);
- } else {
- Node N = new Node(data);
- N.next = first;
- first = N;
- }
- length++;
- }
- /**
- * Creates a new last element
- * @param data the data to insert at the
- * end of the list
- * @postcondition inserts a new given data at the end of the list and creates new last
- */
- public void addLast(int data) {
- if(length==0) {
- first = last = new Node(data);
- } else {
- Node temp = new Node(data);
- last.next = temp;
- last = temp;
- }
- length++;
- return;
- }
- /**
- * removes the element at the front of the list
- * @precondition length != 0
- * @postcondition removes the first element and makes the next element in the list the new first
- * @throws NoSuchElementException when precondition is violated
- */
- public void removeFirst() throws NoSuchElementException{
- if(length == 0) {
- throw new
- NoSuchElementException("removeFirst(): Cannot remove from an empty List!");
- } else if (length == 1) {
- first = last = null;
- } else {
- first = first.next;
- }
- length--;
- return;
- }
- /**
- * removes the element at the end of the list
- * @precondition length != 0
- * @postcondition removes the last node and makes the prior element before the last the new last element of the list
- * @throws NoSuchElementException when precondition is violated
- */
- public void removeLast() throws NoSuchElementException {
- //add your if statement here
- if (length == 0) {
- throw new NoSuchElementException("removeLast(): Cannot remove from an empty List!");
- }
- else if (length == 1) {
- //add a line of code here
- first = last = null;
- } else {
- Node temp = first;
- while (temp.next != last) {
- temp = temp.next;
- }
- last = temp;
- last.next = null;
- }
- length--;
- return;
- }
- /****ADDITIONAL OPERATIONS****/
- /**
- * List with each value separated by a blank space
- * At the end of the List a new line
- * @return the List as a String for display
- */
- @Override public String toString() {
- String result = "";
- Node temp = first;
- while(temp != null) {
- result += temp.data + " ";
- temp = temp.next;
- //fill in here
- }
- result += "\n";
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement