java栈的实现
程序员文章站
2022-07-10 20:29:41
...
一.数组实现的栈,能存储任意类型的数据。
/**
* java 使用数组来实现栈,能存储任意数据
*
* @author Linging
* @date 2019/2/10
*
*/
import java.lang.reflect.Array;
public class ArrayStack<T> {
private final static int DEFAULT_SIZE = 10; //栈的默认大小
private T[] mArray; //栈的数组结构
private int top; //栈“指针”
private int count; //栈中元素的个数
public ArrayStack(Class<T> type) {
//调用下面的构造函数
this(type, DEFAULT_SIZE);
}
public ArrayStack(Class<T> type, int size) {
//通过java反射机制创建动态数组
[链接](https://blog.csdn.net/e421083458/article/details/8839311)
mArray = (T[])Array.newInstance(type, DEFAULT_SIZE);
count = 0;
top = -1;
}
//进栈
public void push(T data) {
mArray[++top] = data;
count++;
}
//判断栈是否为空
public boolean isEmpty() {
return top == -1;
}
//判断栈是否满
public boolean isFull() {
return count == DEFAULT_SIZE;
}
//出栈
public T pop() {
T m = mArray[top];
top--; count--;
return m;
}
//获取栈顶元素,不删除栈顶元素
public T peek() {
return mArray[count-1];
}
//打印栈
public void print() {
if(!isEmpty()) {
for(int i = 0; i <= top; i++) {
System.out.println(mArray[i]);
}
}
System.out.println("count="+count);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayStack<String> stack = new ArrayStack<String>(String.class);
//依次进栈10,20,30
System.out.println("依次进栈:");
stack.push("10");
stack.push("20");
stack.push("30");
//打印栈
stack.print();
//获取栈顶元素
System.out.println("栈顶元素为:"+stack.peek());
//判断栈是否为空
System.out.println("isEmpty:"+stack.isEmpty());
//判断栈是否为满
System.out.println("isFull:"+stack.isFull());
//依次出栈
System.out.println("\n"+"依次出栈:");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.print();
System.out.println("isEmpty:"+stack.isEmpty());
System.out.println("isFull:"+stack.isFull());
}
}
二.java的Collection中的stack演示。
/**
* java Collection中的stack
*
* @date 2019/2/10
* @author Linging
*
*/
import java.util.Stack;
public class CollectionStack {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> stack = new Stack<String>();
//进栈
stack.push("100");
stack.push("200");
stack.push("300");
//输出栈顶元素,不删除栈顶元素。
System.out.println("1栈顶元素为:"+stack.peek());
//输出栈顶元素,删除栈顶元素。
System.out.println("2栈顶元素为:"+stack.pop());
System.out.println("栈中元素个数:"+stack.size());
stack.push("400");
//依次出栈
System.out.println("依次出栈:");
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("栈中元素个数:"+stack.size());
}
}
上一篇: Redis的HyperLogLog
下一篇: java实现栈