Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jasonandrewsca3;
- import java.util.ListIterator;
- public class IteratorList<E> {
- private IteratorList list;
- //private Node next = (Node) list.next();
- private int length = 0;
- private Node head;
- private Node tail;
- Node<E> current = head;
- public ListIterator i;
- // your own class is going to give this out
- public IteratorList()
- {
- current = null;
- }
- public IteratorList(Node current)
- {
- this.current = current;
- }
- public boolean isEmpty() {
- if (length == 0){
- //System.out.print("Yes");
- return true;
- } else {
- //System.out.print("No");
- return false;
- }
- }
- /*public int size() {
- return length;
- }*/
- public Node<E> size(int index) throws IndexOutOfBoundsException {
- if (index < 0 || index > length) {
- throw new IndexOutOfBoundsException();
- } else {
- Node<E> cursor = head;
- for (int i = 0; i < index; i++) {
- cursor = cursor.getNext();
- }
- return cursor;
- }
- }
- public boolean hasNext() {
- if(head.next != null) {
- //System.out.print("Is there any next object? Answer = ");
- return true;
- }
- else {
- //System.out.print("Is there any next object? Answer = ");
- return false;
- }
- }
- public boolean hasPrevious() {
- if(current.prev != null){
- //System.out.print("Is there any previous object? Answer = ");
- return true;
- }
- else {
- //System.out.print("Is there any previous object? Answer = ");
- return false;
- }
- }
- public Object next()
- {
- if(!hasNext()) {
- return null;
- }
- current = current.next;
- return current.data;
- }
- public Object previous()
- {
- if(!hasPrevious()) {
- return null;
- }
- current = current.prev;
- return current.data;
- }
- Node newNode = new Node();
- public int nextIndex() throws IndexOutOfBoundsException{
- if(!current.next(newNode))
- {
- return (int)current.next();
- }
- else{
- return length;
- }
- }
- public int previousIndex() {
- throw new IndexOutOfBoundsException("Not supported yet.");
- }
- /*public void remove() {
- length--;
- }*/
- public void remove(Node node) throws IndexOutOfBoundsException{
- // Handle the case when removing the head node from the list.
- if( node.prev == null ) {
- head = node.next; // Same as head = head.next
- } else {
- node.prev.next = node.next;
- }
- // Handle the case when removing the tail node from the list.
- if( node.next == null ) {
- tail = node.prev; // Same as tail = tail.prev
- } else {
- node.next.prev = node.prev;
- }
- length--;
- }
- public void set(int value, E e) throws IndexOutOfBoundsException {
- }
- public void add(int index, E value) throws IndexOutOfBoundsException {
- Node<E> cursor = size(index);
- Node<E> temp = new Node<>(value);
- temp.setPrev(cursor);
- temp.setNext(cursor.getNext());
- cursor.getNext().setPrev(temp);
- cursor.setNext(temp);
- length++;
- }
- public void insertBefore(Node node, Node insert)
- {
- Node prevNode = node.prev;
- insert.next = node;
- insert.prev = prevNode;
- if(prevNode != null){
- prevNode.next = insert;
- }
- node.prev = insert;
- }
- @Override
- public String toString() {
- StringBuffer result = new StringBuffer();
- Node<E> temp = head;
- int index = list.length;
- while (temp.getNext() != list.size(index)) {
- temp = temp.getNext();
- result.append(temp.getValue()).append(" - ");
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment