Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The Class CustomLinkedList.
- * It's a linkedlist. It adds, adds to the end, shows the next and checks if there's a next.
- * Hell, checks if it's empty, and even gives size.
- * @param <T> the generic type
- * @author Nikolas McGovern
- * @author Samuel Schoeneberger
- */
- public class CustomLinkedList<T> {
- /** The head node. */
- private Node head;
- /** The iterator node. */
- private Node iterator;
- /**
- * Instantiates a new CustomLinkedList.
- * sets head to a node with null properties
- */
- public CustomLinkedList() {
- head = new Node(null, null);
- resetIterator();
- }
- /**
- * Reset iterator.
- */
- public void resetIterator() {
- iterator = head;
- }
- /**
- * Checks if there's a next node.
- * @return true, if successful
- */
- public boolean hasNext() {
- return iterator.hasNext();
- }
- /**
- * returns the next Node.
- * @return the next node.
- */
- public T next() {
- if (iterator.hasNext()) {
- return iterator.next();
- } else {
- return null;
- }
- }
- public void add(T t) {
- if (contains(t)) {
- remove(t);
- }
- Node newNode = new Node(t, head);
- head = newNode;
- }
- public int indexOf(T t) {
- for (int i = 0; i < size(); i++) {
- if (lookAtItemN(i).equals(t)) {
- return i;
- }
- }
- return -1;
- }
- public boolean contains(T t) {
- return indexOf(t) != -1;
- }
- /**
- * Look at item at position index.
- * @param index the index to look at.
- * @return the node at position index
- */
- public T lookAtItemN(int index) {
- if (index < 0) {
- return null;
- }
- iterator.resetIterator();
- for (int i = 0; i < index; i++) {
- iterator.next();
- }
- return iterator.data;
- }
- /**
- * Search for node with this data.
- * @param data to look for
- * @return the node with that data.
- */
- public T searchForNodeData(T t) {
- for (int i = 0; i < size(); i++) {
- if (lookAtItemN(i).equals(t)) {
- return lookAtItemN(i);
- }
- }
- return null;
- }
- public T remove(T t) {
- resetIterator();
- while(iterator.hasNext()) {
- if (iterator.next.data == null) {
- return t;
- }
- if (iterator.next.data.equals(t)) {
- iterator.next = iterator.next.next;
- }
- iterator = iterator.next;
- }
- return t;
- }
- /**
- * Returns size of list.
- * @return the size of list.
- */
- public int size() {
- if (head == null) return 0;
- int size = 0;
- resetIterator();
- while (iterator.hasNext()) {
- size++;
- iterator.next();
- }
- resetIterator();
- return size;
- }
- public String output() {
- String s = "";
- for (int i = 0; i < size(); i++) {
- s += lookAtItemN(i) + " ";
- }
- return s;
- }
- public void moveToFront(T t) {
- resetIterator();
- // System.out.println("MTF 0: " + t);
- // System.out.println("MTF 1: " + output());
- this.remove(t);
- // System.out.println("MTF 2: " + output());
- this.add(t);
- // System.out.println("MTF 3: " + output());
- }
- /**
- * The Private Class Node.
- * Handles next, hasNext, resetIterator on its own.
- */
- private class Node {
- /** The data. */
- public T data;
- /** The next node. */
- public Node next;
- /**
- * Instantiates a new node.
- * @param t the data
- * @param n the next node
- */
- public Node(T t, Node n) {
- data = t;
- next = n;
- }
- /**
- * Reset iterator.
- */
- public void resetIterator() {
- iterator = head;
- }
- /**
- * Returns the next data.
- * @return the next node's data.
- */
- public T next() {
- if (hasNext()) {
- iterator = next;
- return iterator.data;
- } else {
- return null;
- }
- }
- /**
- * Checks if node has a next node.
- * @return true, if successful
- */
- public boolean hasNext() {
- return iterator.next != null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement