<jupyter_start><jupyter_text>Linked lists, two versions , using a linked list and using an array<jupyter_code>class LinkedStackOfStrings {
    private Node first = null;
    private class Node {
        String item;           // private inner class of linked items
        Node next;             // this is effectively a pointer to another item in the linked list
    }                          // the list starts off empty so first is set to null.
    public boolean isEmpty() {
        return first == null;
    }
    public void push(String item) {
        Node oldFirst = first;
        first = new Node();  // create a new node for each item and make sure it points back to the 
        first.item = item;    // previous item
        first.next = oldFirst;
    }
    public String pop() {
        if(isEmpty()) {
            return new String("Stack is empty");
        }
        String item = first.item;  // get the current item string
        first = first.next;         // set first to now point to the next item in the list
        return item;
    }
}<jupyter_output><empty_output><jupyter_text>Check by creating the class and adding items to it and then retrieving an item from it.   Does it seem act like a stack.<jupyter_code>LinkedStackOfStrings s = new LinkedStackOfStrings();
s.push("times");
s.push("are");
s.push("changing");
System.out.println(s.pop());<jupyter_output>changing<jupyter_text>Can we achieve the same thing in an array ?<jupyter_code>class ArrayStackOfStrings {
    public String[] strs;
    public int position;
    
    public ArrayStackOfStrings(int size) {
        position = 0;
        strs = new String[size];  
    }
    public boolean isEmpty() {
        return strs[position - 1] == null;
    }
    public void push(String item) {
        strs[position++] = item;
    }
    public String pop() {
        if(isEmpty()) {
            return new String("Stack is empty");
        }
        return strs[--position];
    }
}
ArrayStackOfStrings alist = new ArrayStackOfStrings(10);
alist.push("if");
alist.push("I had");
alist.pop();
alist.pop();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.097918637653737, 'fraction_numerical': 0.001892147587511826, 'mean_word_length': 2.4502446982055464, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '521460', 'license': 'MIT', 'n_tokens_mistral': 603, 'n_tokens_neox': 596, 'n_words': 211, 'path': 'stacks_and_linked_lists.ipynb', 'repo_name': 'andrewdonov/java_algo_notebooks', 'size': '2114'}