Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- import java.util.*;
- /**
- * A class to represent a linked list of nodes.
- */
- public class LinkedList<T> implements Iterable<T> {
- /** the first node of the list, or null if the list is empty */
- private LLNode<T> front;
- /**
- * Creates an initially empty linked list
- */
- public LinkedList() {
- front = null;
- }
- /**
- * Returns the first node.
- */
- protected LLNode<T> getFront() {
- return front;
- }
- /**
- * Changes the first node.
- * @param node the first node of the new linked list
- */
- protected void setFront(LLNode<T> node) {
- this.front = node;
- }
- public ArrayList toArrayList(){
- LLNode<T> nodeptr = getFront();
- ArrayList<T> list = new ArrayList<T>();
- for (int i = 0; i < length(); i++){
- list.add(i, nodeptr.getElement());
- nodeptr = nodeptr.getNext();
- }
- return list;
- }
- /**
- * Add an element to the front of the linked list
- */
- public void addToFront(T element) {
- setFront(new LLNode<T>(element, getFront()));
- }
- /**
- * Return whether the list is empty
- * @return true if the list is empty
- */
- public boolean isEmpty() {
- return (getFront() == null);
- }
- /**
- * Returns the length of the linked list
- * @return the number of nodes in the list
- */
- public int length() {
- int count = 0; // counts number of nodes seen
- LLNode<T> nodeptr = getFront();
- while (nodeptr != null) {
- count++;
- nodeptr = nodeptr.getNext();
- }
- return count;
- }
- /**
- * Remove and return the element at the front of the list
- * @return the first element of the list
- * @throws NoSuchElementException if there is no such element
- */
- public T removeFromFront() {
- if (isEmpty())
- throw new NoSuchElementException();
- else {
- T save = getFront().getElement();
- setFront(getFront().getNext());
- return save;
- }
- }
- /**
- * Add an element to the very end of the list
- * @param element the element to add to the end of the list
- */
- public void addToEnd(T element) {
- if (isEmpty())
- addToFront(element);
- else {
- LLNode<T> nodeptr = getFront();
- // the loop will end with nodeptr looking at the last node in list
- while (nodeptr.getNext() != null)
- nodeptr = nodeptr.getNext();
- nodeptr.setNext(new LLNode<T>(element, null));
- }
- }
- /**
- * Required by the Iterable interface.
- * @return an LLiterator for the list
- */
- public LLIterator<T> iterator() {
- return new LLIterator<T>(getFront());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement