Java 实现栈的三种方式

栈:LIFO(后进先出),自己实现一个栈,要求这个栈具有push()pop()(返回栈顶元素并出栈)、peek() (返回栈顶元素不出栈)、isEmpty()这些基本的方法。

一、采用数组实现栈

提示:每次入栈之前先判断栈的容量是否够用,如果不够用就用Arrays.copyOf()进行扩容

import java.util.Arrays;
/**
 * 数组实现栈
 * @param 
 */
class Mystack1 {
    //实现栈的数组
    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 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(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
    }
}

二、采用链表实现栈

/**
 * 链表实现栈
 *
 * @param 
 */
class Mystack2 {
    //定义链表
    class Node {
        private T t;
        private Node next;
    }

    private Node head;

    //构造函数初始化头指针
    Mystack2() {
        head = null;
    }

    //入栈
    public void push(T t) {
        if (t == null) {
            throw new NullPointerException("参数不能为空");
        }
        if (head == null) {
            head = new Node();
            head.t = t;
            head.next = null;
        } else {
            Node temp = head;
            head = new Node<>();
            head.t = t;
            head.next = temp;
        }
    }

    //出栈
    public T pop() {
        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");
        System.out.println(stack.peek());
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
    }
}

三、采用LinkedList实现栈

  • push-----addFirst()
  • pop-------removeFirst()
  • peek-----getFirst()
  • isEmpty-isEmpty()
import java.util.LinkedList;

/**
 * LinkedList实现栈
 *
 * @param 
 */
class ListStack {
    private LinkedList 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 stack = new ListStack();
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
        stack.push("java");
        stack.push("is");
        stack.push("beautiful");
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());
    }
}

 

你可能感兴趣的:(Java,基础)