Definition and implementation of stack

Posted by Tonsil on Sat, 07 Sep 2019 20:49:58 +0200

Preface

  • Language: Java
  • Environment: IntelliJ IDEA
  • JDK version: 1.8
  • Source code: GitHub

Definition of stack

A stack, also known as a stack, is a linear table with limited operations.Limits a linear table to insert and delete only at the end of the table.This end is called the top of the stack, while the other end is called the bottom.Inserting a new element into a stack, also known as a stack, a stack, or a stack, places the new element on top of the stack to make it a new top element; deleting an element from a stack, also known as a stack-out or a stack-down, deletes the top element and makes its adjacent elements a new top element.

Queue characteristics:

  • FILO, First in stack, Last out stack, Then in stack, First out stack
  • Whether implemented as an array or a linked list, you usually need a variable (pointer) to mark the top of the stack

Provisions in the queue:

  • The stacking method is named push
  • Stack out method name is pop
  • The variable pointing to the top of the stack (pointer) is named top

Implementation of stack

Array implementation stack

There are several cases where an array implements a queue:

The following information can be obtained from the above four scenarios:

  • Initial value top=-1
  • Top points to top of stack data
  • If the stack is empty, top==-1
  • The criteria for determining a full queue is top==maxSize-1
  • The number of valid data is top+1
  • Add top before inserting data at top
  • Remove the data pointed to by top before leaving the queue to make top-1
public class ArrayStack {
    private int maxSize;   //Maximum capacity of stack array
    public int length = 0;  //Number of data in the stack
    private int top = -1;   //Top stack index, default to -1, pointing to the topmost data
    private Employee[] stack;   //Stack array

    public ArrayStack(){
        this.maxSize = 10;
        this.stack = new Employee[this.maxSize];
    }

    public ArrayStack(int maxSize){
        this.maxSize = maxSize;
        this.stack = new Employee[this.maxSize];
    }

    /**
     * Push
     */
    public boolean push(Employee employee){
        if (isFull()){ return false; }
        this.top++;
        this.stack[this.top] = employee;
        this.length++;
        return true;
    }

    /**
     * Stack Out
     */
    public Employee pop(){
        if(isEmpty()){ return null; }
        Employee employee = this.stack[this.top];
        this.top--;
        this.length--;
        return employee;
    }
    /**
     * Is stack empty
     */
    public boolean isEmpty(){
        return this.top == -1;
    }

    /**
     * Is the stack full
     */
    public boolean isFull(){
        return this.top == this.maxSize-1;
    }

    /**
     *  Format all data in the display stack
     */
    public String formatStack(){
        if (isEmpty()){ return "[]";}
        String str = "";
        for (int i = this.length-1;i>=0;i--){
            str += this.stack[i].toString()+"\n";
        }
        return str;
    }
}

Advantage:

  • Easy to understand and implement

Disadvantages:

  • Stack has a limited amount of data
  • Directly specifying the size of the array is not conducive to the rational use of memory space

Chain List Implementation Stack

There are several cases where a chain table implements a queue:

The following information can be obtained from the above four scenarios:

  • Initial value top=head
  • next of head points to top of stack data
  • If the stack is empty, head.next==null
  • The number of valid data is length
  • Queue entry determines if the stack is empty. If it is empty, the data is placed directly in head.next. If it is not empty, the data needs to be inserted after the head node
  • Save the data pointed to by top before queuing, point head.next to top.next, and move top to the next data on the stack
public class LinkedStack {
    private Node top;   //Point to top of stack
    private Node head;  //head node
    private int length = 0; //Number of data in stack
    public LinkedStack(){
        this.head = new Node();
        this.top = this.head;
    }

    /**
     * Push
     */
    public boolean push(Employee employee){
        Node node = new Node(employee);
        if(isEmpty()){
            this.head.next = node;
            this.top = node;
            length++;
            return true;
        }
        node.next = this.top;
        this.head.next = node;
        this.top = node;
        length++;
        return true;
    }

    /**
     * Stack Out
     */
    public Employee pop(){
        if(isEmpty()){ return null; }
        Employee employee = this.top.data;
        this.head.next = this.top.next;
        this.top = this.top.next;
        length--;
        return employee;
    }
    /**
     * Is stack empty
     */
    public boolean isEmpty(){
        return this.head.next == null;
    }

    /**
     *  Format all data in the display stack
     */
    public String formatStack(){
        if (isEmpty()){ return "[]";}
        Node temp = this.head.next;
        String str = "";
        while (temp!=null){
            str += temp.data.toString() + "\n";
            temp = temp.next;
        }
        return str;
    }

    /**
     * node
     */
    class Node{
        private Employee data;
        private Node next;
        public Node(){}
        public Node(Employee employee){
            this.data =employee;
        }
    }
}

Advantage:

  • Unlimited stack capacity
  • Reasonable use of memory resources by using JVM's garbage collection mechanism

Topics: Programming Java IntelliJ IDEA JDK github