数组实现栈和队列
原创
©著作权归作者所有:来自51CTO博客作者放下也不自在的原创作品,请联系作者获取转载授权,否则将追究法律责任
public class ArrayToQueueAndStack {
public static class MyStack<T> {
public MyStack(int limit) {
this.arr = new Object[limit];
public void push(T value) {
System.out.println("the stack is full");
System.out.println("the stack is empty");
public boolean isEmpty() {
public static class MyQueue<T> {
public MyQueue(int limit) {
this.arr = new Object[limit];
public void push(T value) {
System.out.println("the queue is full");
pushIndex = nextIndex(pushIndex);
System.out.println("the queue is empty");
T value = (T) arr[pollIndex];
pollIndex = nextIndex(pollIndex);
public boolean isEmpty() {
private int nextIndex(int pushIndex) {
return ++pushIndex % limit;