package com.hithomelabs.princeton1.module4;

import javax.annotation.Nonnull;
import java.util.Iterator;

// Creating a concrete linked Implementation of Stack
public class LinkedStack<E> extends Stack<E>{

    static class Node<E>{
        E data;
        Node<E> next;

        Node(E data) {
            this.data = data;
            this.next = null;
        }
    }

    Node<E> head = null;
    int stackLength = 0;

    @Override
    public boolean isEmpty() {
        return head == null;
    }

    @Override
    public void push(E element) {
        Node<E> new_node = new Node<E>(element);
        new_node.next = head;
        head = new_node;
        stackLength++;
    }

    @Override
    public E pop() {
        if(stackLength == 0)
            return null;
        E currentValue = head.data;
        head = head.next;
        return currentValue;
    }

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

    @Nonnull
    @Override
    public Iterator<E> iterator() {
        return new LinkedlistIterator();
    }

    private class LinkedlistIterator implements Iterator<E>{

        Node<E> iteratorPtr = head;

        @Override
        public boolean hasNext() {
            return iteratorPtr.next != null;
        }

        @Override
        public E next() {
            E currentValue = iteratorPtr.data;
            iteratorPtr = iteratorPtr.next;
            return currentValue;
        }
    }
}
