Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package structures;
- import java.util.Iterator;
- public class RecursiveList<T> implements ListInterface<T> {
- private class Node<T>{
- T data;
- Node<T> next;
- public Node(T data) {
- this.data = data;
- this.next = null;
- }
- }
- private class NodeIterator<T> implements Iterator<T>{
- private Node<T> curr;
- public NodeIterator() {
- curr = front;
- }
- public boolean hasNext() {
- return curr.next != null;
- }
- public T next() {
- T temp = curr.data;
- curr = curr.next;
- return temp;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
- private int size;
- public Node front;
- public <T> RecursiveList() {
- this.size = 0;
- front = new Node<T>(null);
- }
- @Override
- public int size() {
- return this.size;
- }
- private Node<T> findNode(int index, int count, Node curr) {
- if (index == count || curr.next == null)
- return curr;
- return findNode(index, count + 1, curr.next);
- }
- @Override
- public ListInterface<T> insertFirst(T elem) {
- if (elem == null)
- throw new NullPointerException();
- return insertAt(0, elem);
- }
- @Override
- public ListInterface<T> insertLast(T elem) {
- if (elem == null)
- throw new NullPointerException();
- if(size() == 0) {
- return insertFirst(elem);
- }
- return insertAt(size(), elem);
- }
- @Override
- public ListInterface<T> insertAt(int index, T elem) throws IndexOutOfBoundsException {
- if ((index < 0) || (index > size))
- throw new IndexOutOfBoundsException();
- if (elem == null)
- throw new NullPointerException();
- Node<T> node = new Node<T>(elem);
- size++;
- if (size == 0) {
- front = node;
- return this;
- }
- if (index == 0) {
- if (size == 0) {
- this.front = node;
- } else {
- node.next = front;
- front= node;
- }
- return this;
- }
- Node<T> prev = findNode(index - 1, 0, front);
- if (index == size) {
- prev.next = node;
- } else {
- Node<T> temp = prev.next;
- prev.next = node;
- node = temp;
- }
- return this;
- }
- @Override
- public T removeFirst() {
- if(isEmpty()) throw new IllegalStateException();
- return removeAt(0);
- }
- @Override
- public T removeLast() {
- if(isEmpty()) throw new IllegalStateException();
- if(size() == 1) {
- return removeFirst();
- }
- return removeAt(size() - 1);
- }
- @Override
- @SuppressWarnings("unchecked")
- public T removeAt(int index) {
- if(isEmpty()) throw new IllegalStateException();
- if (index < 0 || index >= size())
- throw new IndexOutOfBoundsException();
- this.size--;
- if(index == 0) {
- T ret = (T) front.data;
- if(size == 1 ) {
- front = null;
- } else {
- front = front.next;
- }
- return ret;
- }
- Node<T> prev = findNode(index - 1, 0, front);
- T ret = prev.next.data;
- prev.next = prev.next.next;
- return ret;
- }
- @Override
- public T getFirst() {
- if(isEmpty()) throw new IllegalStateException();
- return get(0);
- }
- @Override
- public T getLast() {
- if(isEmpty()) throw new IllegalStateException();
- return get(size() - 1);
- }
- @Override
- public T get(int index) {
- if(isEmpty())
- throw new IllegalStateException();
- if (index < 0 || index >= size())
- throw new IndexOutOfBoundsException();
- return findNode(0, index, front).data;
- }
- public T peek() {
- return (T) front.data;
- }
- @Override
- public boolean remove(T elem) {
- if(elem == null)
- throw new NullPointerException();
- boolean del = removeHelper(front, 0, elem);
- return del;
- }
- private boolean removeHelper(Node<T> curr, int index, T elem) {
- if(index > size() || curr == null) return false;
- if(curr.data == elem) {
- removeAt(index);
- return true;
- }
- return removeHelper(curr.next, index + 1, elem);
- }
- @Override
- public int indexOf(T elem) {
- if(elem == null)
- throw new NullPointerException();
- // if(elem == front.data) return 0;
- return indexHelper(front, 0, elem);
- }
- private int indexHelper(Node<T> curr, int index, T elem) {
- if(curr.equals(null)) return -1;
- if(curr.data.equals(elem)) {
- return index;
- }
- return indexHelper(curr.next, index + 1, elem);
- }
- @Override
- public boolean isEmpty() {
- return size() <= 0;
- }
- @Override
- public Iterator<T> iterator() {
- // TODO Auto-generated method stub
- return new NodeIterator<T>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement