java代码简单实现栈

1. 基于数组简单实现

 1 /**
 2  * @author <a herf="mailto:yanwu0527@163.com">XuBaofeng</a>
 3  * @date 2019-11-29 14:37.
 4  * <p>
 5  * description: 基于数组实现栈
 6  */
 7 public class ArrayStack<T> {
 8     private static final Integer DEFAULT_SIZE = 10;
 9     /*** 栈 */
10     private T[] items;
11     /*** 栈顶所在角标 */
12     private int top = -1;
13     /*** 栈总容量 */
14     private int size;
15 
16     public ArrayStack() {
17         this(DEFAULT_SIZE);
18     }
19 
20     public ArrayStack(int len) {
21         if (len <= 0) {
22             throw new IndexOutOfBoundsException("new instance error, len need more than 0, size: " + len);
23         }
24         size = len;
25         items = (T[]) new Object[size];
26     }
27 
28     /**
29      * 判断栈是否为控
30      *
31      * @return
32      */
33     public boolean isEmpty() {
34         return top == -1;
35     }
36 
37     /**
38      * 压栈
39      *
40      * @param t
41      * @return
42      */
43     public boolean push(T t) {
44         if (top == size - 1) {
45             return false;
46         }
47         items[++top] = t;
48         return true;
49     }
50 
51     /**
52      * 弹栈
53      *
54      * @return
55      */
56     public T pop() {
57         if (isEmpty()) {
58             return null;
59         }
60         T item = items[top];
61         items[top--] = null;
62         return item;
63     }
64 
65     /**
66      * 获取栈顶元素
67      *
68      * @return
69      */
70     public T top() {
71         if (isEmpty()) {
72             return null;
73         }
74         return items[top];
75     }
76 
77     public static void main(String[] args) {
78         ArrayStack<Integer> stack = new ArrayStack<>();
79         for (int i = 0; i < DEFAULT_SIZE + 1; i++) {
80             System.out.println("push: " + stack.push(i) + ", item: " + i);
81         }
82         for (int i = 0; i < DEFAULT_SIZE + 1; i++) {
83             System.out.println("top: " + stack.top() + ", pop: " + stack.pop());
84         }
85     }
86 }

2. 基于链表简单实现

 1 import lombok.AllArgsConstructor;
 2 import lombok.Data;
 3 
 4 /**
 5  * @author <a herf="mailto:yanwu0527@163.com">XuBaofeng</a>
 6  * @date 2019-12-10 17:27.
 7  * <p>
 8  * description: 基于链表实现栈
 9  */
10 public class LinkedStack<T> {
11     private static final Integer DEFAULT_SIZE = 10;
12     /*** 栈顶元素 */
13     private Node top;
14     /*** 栈当前容量 */
15     private Integer index;
16     /*** 栈总容量 */
17     private Integer size;
18 
19     public LinkedStack() {
20         this(DEFAULT_SIZE);
21     }
22 
23     public LinkedStack(Integer len) {
24         if (len <= 0) {
25             throw new IndexOutOfBoundsException("new instance error, len need more than 0, size: " + len);
26         }
27         index = -1;
28         size = len;
29     }
30 
31     /**
32      * 判断栈是否为空
33      *
34      * @return
35      */
36     public Boolean isEmpty() {
37         return top == null;
38     }
39 
40     /**
41      * 压栈
42      *
43      * @param t
44      * @return
45      */
46     public boolean push(T t) {
47         if (index >= size - 1) {
48             return false;
49         }
50         Node old = top;
51         top = new Node(t, old);
52         index++;
53         return true;
54     }
55 
56     /**
57      * 弹栈
58      *
59      * @return
60      */
61     public Node pop() {
62         if (isEmpty()) {
63             return null;
64         }
65         Node result = top;
66         top = top.next;
67         index--;
68         return result;
69     }
70 
71     /**
72      * 获取栈顶元素
73      *
74      * @return
75      */
76     public Node top() {
77         return top;
78     }
79 
80     @Data
81     @AllArgsConstructor
82     private class Node {
83         private T data;
84         private Node next;
85     }
86 
87     public static void main(String[] args) {
88         LinkedStack<String> stack = new LinkedStack<>();
89         for (int i = 0; i < DEFAULT_SIZE + 1; i++) {
90             System.out.println("push: " + stack.push(String.valueOf(i)) + ", item: " + i);
91         }
92         for (int i = 0; i < DEFAULT_SIZE + 1; i++) {
93             System.out.println("top: " + stack.top());
94             System.out.println("pop: " + stack.pop());
95         }
96     }
97 }
原文地址:https://www.cnblogs.com/yanwu0527/p/11957982.html