简体   繁体   中英

Doubly Generic Linked List with anonymous inner class (in function)

i deleted my last post and i rewrite the problem again (after i learn how to do it and what is the requirements.

1.I need to define Nodedouble as a generic inner class - private. In this class I need to write constructor that consume data and string method.

2.I need to define Linkedlistdouble in this class in need constructer without elements. Methode to add an element And method to present the list.

3.In the class LinkedListdouble i need to define method listiterator that return object from type listiteartor. In the functiob listiterator I need return 3 method: - T next - return the next element - Boolean has next-return true if the list iteartor has more elements - T previous- return the previous element in the list, if there is not an element - return exception..

this is my code:

enter code here

public class DoublyLinkedList<T> {
/**
 * 
 * @author USER
 *
 * @param <T>
 */
    class Node <T>{

        private T data;
        private Node previous;
        private Node next;

        Node(T data) { 
            this.data = data; 

        }
        public Node previous() { 
            return this.previous; 
        }

        public Node next() { 
            return this.next; 
        }

        public T getContent() { 
            return this.data; 
        }

        public void setPrevious(Node previous) { 
            this.previous = previous; 
        }
        public void setNext(Node next) { 
            this.next = next; 
        }

        public String toString() { 
            return data.toString(); 
            }
    }


    private Node head;
    private Node tail;
    private int size = 0;

    public Node head() { 
        return this.head;
        }

    public Node tail() {
        return this.tail; 
        }

    public void add(T element) {
        Node newNode = new Node(element);
        if (this.size == 0) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.setNext(newNode);
            newNode.setPrevious(this.tail);
            this.tail = newNode;
        }
        this.size++;
    }
    /*
     public boolean isEmpty(){

            return size==0;

        }
      public void push(int data){
            Node n= new Node(data);
            if(isEmpty()) {
                tail=n;
                head=n;
            }
            else {
               head.previous = n;
               n.next=head;
                head=n;
            }
            size++;
        }*/


    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        Node currentNode = this.head;
        while (currentNode != null) {
            stringBuilder.append(currentNode.getContent()).append(" ");
            currentNode = currentNode.next();
        }
        System.out.println(stringBuilder.toString().trim());
        return stringBuilder.toString().trim();
    }

    public ListIterator listIterator() {

        return new ListIterator() {
            Node curr;

            public boolean hasNext() {
                return curr.next() != null;
            }

            public T next() {
                T element = (T) curr.getContent();
                curr = curr.next();
                return element;
            }
            public T previous()  {
                T element = (T) curr.getContent();
                if(curr.previous()!=null) {
                curr = curr.previous();
                return element;
                }
                throw new NoSuchElementException();
            }
            @Override
            public boolean hasPrevious() {
                if(curr.previous() != null) {
                    return true;
                }
                return false;
            }
            @Override
            public void add(Object e) {
             throw new UnsupportedOperationException();             
            }
            @Override
            public int nextIndex() {
                throw new UnsupportedOperationException();              
            }
            @Override
            public int previousIndex() {
                throw new UnsupportedOperationException();              
            }
            @Override
            public void remove() {
                throw new UnsupportedOperationException();              

            }

            @Override
            public void set(Object e) {
                throw new UnsupportedOperationException();              

            }
        };

    }
     public static void main (String[]args) {
         DoublyLinkedList<Integer> a = new DoublyLinkedList();
            a.add(7);
            a.add(8);
            a.add(9);
            a.add(10);
            a.toString();

            ListIterator <Integer> iter = a.listIterator();
            while (iter.hasNext()) {
                System.out.println(iter.next());

            }

     }
        }

my questions: 1. i get a wrong when i try do do this code in the main: how i can fix it? (the main is above)

  1. i am reading some articles about anonymous inner class, I don't know if i indeed do it right in this exercise (in the function listiterator i try to define anonymous inner class but i don't know if this is the way to do it..

thank you! have a good day

Couple of changes to your listIterator:

Node curr = head // otherwise it is null

Change curr.next() to cur in the hasNext() method, otherwise you don't get the last element.

public boolean hasNext() {
     return curr != null;
}

Hope that helps

RR

The most straight forward way to do it would be just to add an argument to the listIterator for the head or the tail:

ListIterator<Integer> iter = a.listIterator(a.getHead());

    while (iter.hasNext()) {
        System.out.println(iter.next());

    }

    iter = a.listIterator(a.getTail());

    while (iter.hasPrevious()) {
        System.out.println(iter.previous());

    }

I modified your ListIterator like so:

public ListIterator listIterator(Node start) {

    return new ListIterator() {
        DoublyLinkedList.Node curr = start;


        public boolean hasNext() {
            //System.out.println(curr);
            return curr != null;
        }

        public T next() {
            T element = (T) curr.getContent();
            curr = curr.next();
            return element;
        }

        public T previous() {
            T element = (T) curr.getContent();
            curr = curr.previous();
            return element;
        }

        @Override
        public boolean hasPrevious() {
            return curr != null;
        }

There may well be better ways of doing this, such has creating your own iterator. RR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM