Definition of stack
A stack is a linear table that restricts operations to only one end of the table. The end that allows the insertion and deletion of elements is called the top of the stack, and the fixed end is called the bottom of the stack. Because the insertion and deletion operations can only be performed at the top of the stack, the biggest feature of the elements in the stack is "first in first out", that is, the elements in the stack first out of the stack at the back.
Stack is divided into sequence stack and chain stack.
Basic operation of stack
- Stack initialization: generate an empty stack;
- Judge stack empty and stack full: judge whether the elements in the stack are empty or full;
- Stack: add elements to the stack;
- Stack out: delete the elements in the stack;
- Read the top of stack element: get the top of stack element.
Using Java generics to implement sequence stack
//Sequential stack public class Mystack<T> { private T[] stack;//Storage elements private int size;//Record the number of elements in the stack private final int DEFAULT_SIZE=10;//Initialization default size is 10 public Mystack() { size = 0; stack = (T[]) new Object[DEFAULT_SIZE]; } //Judge whether the stack is empty public boolean isEmpty() { if (size <= 0) return true; else return false; } //Determine whether the stack is full public boolean isFull() { if (size >= stack.length) return true; else return false; } //Push public void push(T value) { if (isFull()) return; stack[size++] = value; } //Stack out public void pop() { if (isEmpty()) return; stack[size - 1] = null; size--; } //Get stack top element public T getTop() { return stack[size - 1]; } }
Here is the test code:
public static void main(String[] args){ Mystack<Integer> mystack = new Mystack<>(); System.out.println("Is the stack empty" + mystack.isEmpty()); mystack.push(1); mystack.push(2); mystack.push(3); System.out.println("The element at the top of the stack is" + mystack.getTop()); mystack.pop(); System.out.println("The element at the top of the stack is" + mystack.getTop()); }
The test results are as follows:
The realization of chain stack
public class LinkStack<T> { //Node class static class Entry { private Object value; private Entry next; public Entry() { } public Entry(Object value) { this.value = value; next = null; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } public Entry getNext() { return next; } public void setNext(Entry next) { this.next = next; } } private Entry head;//Header node private Entry tailflag;//Tail node public LinkStack() { head = new Entry(); tailflag = head; } //Tail entry public void pushtail(T value) { Entry newEntry = new Entry(value); tailflag.setNext(newEntry); tailflag = newEntry; } //Trailing stack public void poptail() { tailflag.setValue(null); Entry p = head; while (p.getNext() != tailflag) { p = p.getNext(); } tailflag = p; p.setNext(null); } //Head in stack public void pushhead(T value) { Entry newEntry = new Entry(value); if (head.getNext() == null) head.setNext(newEntry); else { newEntry.setNext(head.getNext()); head.setNext(newEntry); } } //Head out of stack public void pophead() { if (head.getNext() == null) return; else { head.getNext().setValue(null); head.setNext(head.getNext().getNext()); } } //Get header element public T getHead() { return (T) head.getNext().value; } //Get tail element public T getTail() { return (T) tailflag.value; } }
Here is the test code:
public static void main(String[] args) { LinkStack<Integer> mystack = new LinkStack<>(); mystack.pushtail(1); mystack.pushtail(2); mystack.pushtail(3); System.out.println("The tail element is"+mystack.getTail()); System.out.println("The head element is"+mystack.getHead()); mystack.pushhead(4); mystack.pushhead(5); mystack.pushhead(6); System.out.println("The tail element is"+mystack.getTail()); System.out.println("The head element is"+mystack.getHead()); }
Test results: