[Data structure] chain stack of stack

public class LinkStack<T> {

    // Define an inner class Node, Node instance represents the node of the chain stack
    private class Node {
        // save the node data
        private T data;
        // a reference to the next node
        private Node next;
        // constructor with no arguments
        public Node() {
        }
        // initialize the constructor for all properties
        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
    // save the top element of the stack
    private Node top;
    // Save the number of nodes already contained in the chain stack
    private int size;
    // create empty chain stack
    public LinkStack() {
        // Empty chain stack, the value of top is null
        top = null;
    }
    // Create a chain stack with the specified data element, the chain stack has only one element
    public LinkStack(T element) {
        top = new Node(element, null);
        size++;
    }
    // return the length of the chain stack
    public int length() {
        return size;
    }
    // push to stack
    public void push(T element) {
        // Let top point to the newly created element, and the next reference of the new element points to the original top element of the stack
        top = new Node(element, top);
        size++;
    }
    // pop the stack
    public T pop() {
        Node oldTop = top;
        // let the top reference point to the element next to the top element of the original stack
        top = top.next;
        // Release the next reference to the top element of the original stack
        oldTop.next = null;
        size--;
        return oldTop.data;
    }
    // Access the top element of the stack, but do not delete the top element
    public T peek() {
        return top.data;
    }
    // Determine if the chain stack is empty
    public boolean empty() {
        return size == 0;
    }
    // clear the chain stack
    public void clear() {
        top = null;
        size = 0;
    }
    public String toString() {
        if (empty()) {
            return "[]";
        } else {
            StringBuilder sb = new StringBuilder("[");
            for (Node current = top; current != null; current = current.next) {
                sb.append(current.data.toString() + ", ");
            }
            int len = sb.length();
            return sb.delete(len - 2, len).append("]").toString();
        }
    }
}
public class LinkStackTest {
    public static void main(String[] args) {
        LinkStack<String> stack = new LinkStack<String>();
        stack.push("aaaa");
        stack.push("bbbb");
        stack.push("cccc");
        stack.push("dddd");
        System.out.println("List elements after initialization: " + stack.toString());
        System.out.println("Access the top element of the stack: " + stack.peek());
        System.out.println("Popping the top element of the stack for the first time: " + stack.pop());
        System.out.println("The stack element after the first pop: " + stack.toString());
        System.out.println("Pop the top element of the stack for the second time: " + stack.pop());
        System.out.println("Stack element after two pops: " + stack.toString());
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325788838&siteId=291194637