0

I am making a double ended queue(deque) and now i am trying call my list and adding data to it. But i have a struggle with the fact that i cannot get non-static variables from static context.

In this case i am trying to access my variables from the Node class that i made.

My question is: How can i access my variables in the Node class without changing my java interface?

Here is my code:

import java.util.Iterator;
import org.w3c.dom.Node;

public abstract class Deque implements DoubleEndedQueue<Object> {

private Node<Object> first;
private Node<Object> last;
private Node<Object> newNode;
private int size = 0;

public Deque() {
    size = 0;
    first = null;
    last = null;
}

private class Node<Object> {

    Object item;
    Node<Object> prec;
    Node<Object> next;
}

public Iterator<Object> iterator() {
    return new Iterator<Object>() {

        private Node<Object> node = first;

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public Object next() {
            Object item = node.item;
            node = node.next;

            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

public static void main(String[] args) {
    DoubleEndedQueue<Object> doubleEndedQueue = new DoubleEndedQueue<Object>() {

        @Override
        public boolean isEmpty() {
            return size == 0;
        }

        @Override
        public int size() {
            return size;
        }

        @Override
        public void pushLeft(Object item) {
            newNode.item = item;
            if (size == 0) {
                first = last = newNode;
            } else {
                newNode.next = first;
                first.prec = newNode;
            }
            first = newNode;
            if (last == null) {
                last = first;
            }
            size++;
        }

        @Override
        public void pushRight(Object item) {
            newNode.item = item;

            if (size == 0) {
                last = first = newNode;
            } else {
                newNode.prec = last;
                last.next = newNode;
            }
            last = newNode;
            if (first == null) {
                first = last;
            }
            ++size;
        }

        @Override
        public Object popLeft() {
            newNode = first;
            first = first.next;

            if (first == null) {
                last = null;
            } else {
                first.prec = null;
            }

            size--;

            return newNode.item;
        }

        @Override
        public Object popRight() {
            newNode = last;
            last = newNode.prec;

            if (last == null) {
                first = null;
            } else {
                last.next = null;
            }

            size--;

            return newNode.item;
        }

        @Override
        public Object changeLeft(int n, Object newItem) {
            newNode = first;
            for (int i = 0; i < n; i++) {
                newNode = newNode.next;
            }
            return newNode.item = newItem;
        }

        @Override
        public Object changeRight(int n, Object newItem) {
            newNode = last;
            for (int i = 0; i < n; i++) {
                newNode = newNode.prec;
            }
            return newNode.item = newItem;
        }

    };
    d.pushLeft("im first");
    d.pushLeft("im second");
    d.pushRight("im third");
    d.pushRight("im fourth");
    d.pushLeft("im fifth");
    d.pushRight("im sixth");
    d.changeLeft(1, "well");
    d.changeRight(2, "this");
    d.changeLeft(1, "was");
    d.changeRight(3, "fun");
    d.popRight();
    d.popLeft();
    for (String i : d) {
        System.out.print(i + " ");
    }

}
}
Axel
  • 81
  • 2
  • 9
  • Where exactly are you getting the error? – Mureinik Oct 05 '17 at 22:45
  • Why do you have and use **two** different Node classes? One is your own private Node class, and the other the `import org.w3c.dom.Node;` class? – Hovercraft Full Of Eels Oct 06 '17 at 00:48
  • @HovercraftFullOfEels ah sorry thats my bad there... i was still in the middle figuring stuff out and should have removed the w3c class... Anyway the duplicate you send me as helped me alot! – Axel Oct 06 '17 at 09:16

0 Answers0