转载原文:https://blog.csdn.net/weixin_43533825/article/details/96708590
栈:LIFO(后进先出),自己实现一个栈,要求这个栈具有push()、pop()(返回栈顶元素并出栈)、peek() (返回栈顶元素不出栈)、isEmpty()这些基本的方法。
推荐使用第三种
一、采用数组实现栈
提示:每次入栈之前先判断栈的容量是否够用,如果不够用就用Arrays.copyOf()进行扩容
import java.util.Arrays; /** * 数组实现栈 * @param <T> */ class Mystack1<T> { //实现栈的数组 private Object[] stack; //数组大小-->栈顶 private int size; Mystack1() { stack = new Object[10];//初始容量为10 } //判断是否为空 public boolean isEmpty() { return size == 0; } //返回栈顶元素 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()); } }
二、采用链表实现栈
package com.icil.elsa.test; /** * 单向链表实现栈 * @param <T> */ class Mystack2<T> { //定义链表 class Node<T> { private T t; private Node next; } private Node<T> head=null; //构造函数初始化头指针 // Mystack2() { // head = null; // } //入栈 public void push(T t) { if (t == null) { 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()); } }
三、采用LinkedList实现栈
- 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; //直接取元素会报异常,需要先判断是否为空 if (!ll.isEmpty()) t = ll.getFirst(); return t; } //栈空 public boolean isEmpty() { return ll.isEmpty(); } } public class LinkedListStack { public static void main(String[] args) { ListStack<String> stack = new ListStack(); System.out.println(stack.isEmpty()); 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()); } }