Reprinted Original: https://blog.csdn.net/weixin_43533825/article/details/96708590
Stack: LIFO (Last In First Out), own realization of a stack, the stack has the required push (), pop () (returns the top element and the stack), peek () (returns the top element not stack), isEmpty ( ) these basic methods.
The third recommended
First, using the array to achieve stack
Tip: first determine the stack before each stack capacity is enough, if not enough to use Arrays.copyOf () for expansion
java.util.Arrays Import; / * * * achieve stack array * @param <T> * / class Mystack1 <T> { // Array stack implemented private Object [] Stack; // array size -> stack private int size; Mystack1 () { Stack = new new Object [ 10 ]; // initial capacity 10 } // determines whether an empty public Boolean isEmpty () { return size == 0 ; } // returns the top element public T PEEK () { T t = null; if (size > 0) t = (T) stack[size - 1]; return t; } //压栈 public void push(T t) { expandCapacity(size + 1); stack[size] = t; size++; } //出栈 public T pop() { T t = peek(); if (size > 0) { stack[size - 1] = null; size--; } return t; } //扩大容量 public void expandCapacity(int size) { int len = stack.length; if (size > len) { size = size * 3 / 2 + 1;//每次扩大50% stack = Arrays.copyOf(stack, size); } } } public class ArrayStack { public static void main(String[] args) { Mystack1<String> stack = new Mystack1<>(); System.out.println(stack.peek()); System.out.println(stack.isEmpty()); stack.push("java"); stack.push("is"); stack.push("beautiful"); stack.push("language"); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); System.out.println("peek-->"+stack.peek()); } }
Second, the realization of the stack using a linked list
com.icil.elsa.test Package; / * * *-way linked list implementation stack * @param <T> * / class Mystack2 <T> { // definition of the linked list class the Node <T> { Private T T; Private the Node Next; } Private the Node <T> head = null ; // constructor initializes head pointer // Mystack2 () { // head = null; // } // stack public void Push (T T) { IF (T == null ) { the throw new NullPointerException("参数不能为空"); } if (head == null) { head = new Node<T>(); head.t = t; head.next = null; } else { Node<T> temp = head; head = new Node<>(); head.t = t; head.next = temp; } } //出栈 public T pop() { if(head==null) { return null; } T t = head.t; head = head.next; return t; } //栈顶元素 public T peek() { T t = head.t; return t; } //栈空 public boolean isEmpty() { if (head == null) return true; else return false; } } public class LinkStack { public static void main(String[] args) { Mystack2 stack = new Mystack2(); System.out.println(stack.isEmpty()); stack.push("java"); stack.push("is"); stack.push("beautiful"); stack.push("language"); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); } }
Third, the use LinkedList achieve stack
- push-----addFirst()
- pop-------removeFirst()
- peek-----getFirst()
- isEmpty-isEmpty()
package com.icil.elsa.test; import java.util.LinkedList; /** * LinkedList实现栈 * * @param <T> */ class ListStack<T> { private LinkedList<T> ll = new LinkedList<>(); //入栈 public void push(T t) { ll.addFirst(t); } //出栈 public T pop() { return ll.removeFirst(); } //栈顶元素 public T peek() { T t = null; //Direct access to the elements will be reported anomaly, it is necessary to determined whether the air IF (! Ll.isEmpty ()) T = ll.getFirst (); return T; } // stack empty public Boolean isEmpty () { return ll.isEmpty () ; } } public class LinkedListStack { public static void main (String [] args) { ListStack <String> Stack = new new ListStack (); the System. OUT .println (stack.isEmpty ()); . the System OUT .println (Stack. PEEK ()); stack.push ("java"); stack.push("is"); stack.push("beautiful"); stack.push("language"); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); System.out.println("pop-->"+stack.pop()); System.out.println("stack.isEmpty-->"+stack.isEmpty()); } }