Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LinkedList<T> implements ListInterface<T> {
- private Node<T> head;
- /***************************************************************************
- * LinkedList()
- * Constructor - Creates an empty list
- **************************************************************************/
- public LinkedList() {
- head = null;
- } // end LinkedList
- /***************************************************************************
- * length()
- * Returns the number of items in the list
- **************************************************************************/
- public int length() {
- int size = 0;
- Node<T> currentLink = head;
- while (currentLink != null) {
- currentLink = currentLink.getNext();
- size = size + 1;
- }
- return size;
- } // end length
- /***************************************************************************
- * isEmpty()
- * Returns true if the length of the list is 0
- **************************************************************************/
- public boolean isEmpty() {
- if (head == null) {
- return true;
- } else {
- return false;
- } // end if/else
- //OR return (head == null);
- } // end isEmpty
- /***************************************************************************
- * add(T item, int position)
- * Adds the given item at the given position
- **************************************************************************/
- public void add(T item, int position) {
- Node<T> addThis = new Node<T>(item);
- Node<T> prev = head;
- int i;
- if(position <= 0) {
- //throw new ListException("Cannot add element before position 1");
- System.out.println("Cannot add element before position 1");
- }
- else if(position == 1) {
- addThis.setNext(head);
- head = addThis;
- } else {
- for(i = 1; i < position-1; i++) {
- prev = prev.getNext();
- if(prev == null) {
- //throw new ListException("Cannot add beyond end of list");
- System.out.println("Cannot add beyond end of list");
- }
- } // end for
- addThis.setNext(prev.getNext());
- prev.setNext(addThis);
- }
- } // end add
- /***************************************************************************
- * get(int position)
- * Returns the item at the given position
- **************************************************************************/
- public T get(int position) {
- Node<T> node = head;
- int i;
- if(position <= 0) {
- //throw new ListException("Cannot retrieve element before position 1");
- System.out.println("Cannot retrieve element before position 1");
- }
- for(i = 1; i < position; i++) {
- node = node.getNext();
- if(node == null) {
- //throw new ListException("Cannot retrieve element past the end of the list");
- System.out.println("Cannot retrieve element past the end of the list");
- }
- }
- return node.getItem();
- } // end get
- /***************************************************************************
- * delete(int position)
- * Deletes the item at the given position.
- **************************************************************************/
- public void delete(int position) {
- Node<T> prev = head;
- Node<T> target;
- int i;
- if(position <= 0) {
- //throw new ListException("Cannot delete an element before the start of the list");
- System.out.println("Cannot delete an element before the start of the list");
- }
- else if(head == null) {
- //throw new ListException("The list is empty - you cannot delete from an empty list");
- System.out.println("The list is empty - you cannot delete from an empty list");
- }
- else if(position == 1) {
- target = head;
- head = head.getNext();
- target.setNext(null);
- } else {
- for(i = 1; i < position-1; i++) {
- prev = prev.getNext();
- if(prev == null) {
- //throw new ListException("Cannot delete an element past the end of the list");
- System.out.println("Cannot delete an element past the end of the list");
- }
- }
- target = prev.getNext();
- if(target == null) {
- //throw new ListException("Cannot delete an element past the end of the list");
- System.out.println("Cannot delete an element past the end of the list");
- }
- prev.setNext(target.getNext());
- target.setNext(null);
- }
- } // end delete
- } // end LinkedList<T> implements ListInterface<T>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement