java stack 数组_Java 数组实现 stack

首先定义 IStack

package cn.com.example.stack;

/**

* Created by Jack on 2017/3/8.

*/

public interface IStack {

//元素出栈,并返回出栈元素

public T pop() throws IllegalAccessException;

//元素入栈

public void push(T element);

//获取栈顶元素

public T peek() throws IllegalAccessException;

//判断栈是否为空

public boolean isEmpty();

// 栈大小

public int size();

public void clear();

}

接着定义 MyStack 实现 IStack接口  并测试

package cn.com.example.stack;

import java.util.Arrays;

/**

* Created by Jack on 2017/3/8.

*/

public class MyStack implements IStack {

private final int DEFAULT_SIZE = 3;

private int size = 0;

private int capacity = 0;

//top指向下一个能够添加元素的位置

private int top = 0;

private Object[] array;

public MyStack() {

this.capacity = this.DEFAULT_SIZE;

this.array = new Object[this.capacity];

this.size = 0;

}

public MyStack(int capacity) {

this.capacity = capacity;

this.array = new Object[this.capacity];

this.size = 0;

}

/**

* 元素出栈,并返回出栈元素

*

* @return

*/

@Override

public Object pop() throws IllegalAccessException {

if (this.size == 0)

throw new IllegalAccessException("stack element empty");

T element = (T) this.array[top - 1];

this.array[top - 1] = null;

this.size--;

this.top--;

return element;

}

/**

* 元素入栈

*

* @param element

*/

@Override

public void push(Object element) {

if (this.size < this.capacity) {

this.array[this.top] = element;

this.top++;

this.size++;

} else {

// 扩容

enlarge();

push(element);

}

}

private void enlarge() {

this.capacity = this.capacity + this.DEFAULT_SIZE;

Object[] newArray = new Object[this.capacity];

System.arraycopy(array, 0, newArray, 0, array.length);

Arrays.fill(array, null);

this.array = newArray;

}

/**

* 获取栈顶元素

*

* @return

*/

@Override

public Object peek() throws IllegalAccessException {

if (this.size == 0)

throw new IllegalAccessException("stack element empty");

return this.array[this.top - 1];

}

/**

* 判断栈是否为空

*

* @return

*/

@Override

public boolean isEmpty() {

return size == 0;

}

/**

* 获取栈大小

*

* @return

*/

@Override

public int size() {

return size;

}

/**

* 清空 栈

*/

@Override

public void clear() {

Arrays.fill(array, null);

this.capacity = this.DEFAULT_SIZE;

this.array = new Object[this.capacity];

this.size = 0;

this.top = 0;

}

}

class MyStackTest {

public static void main(String[] args) throws IllegalAccessException {

MyStack stack = new MyStack();

stack.push("1");

// 栈头

System.out.println(stack.peek());

// 栈头出栈

System.out.println(stack.pop());

// 是否为空

System.out.println(stack.isEmpty());

System.out.println(stack.size());

for (int i = 1; i <= 10; i++) {

stack.push("" + i);

}

System.out.println(stack.size());

for (int i = 0; i < 10; i++) {

String s = (String) stack.pop();

System.out.println(s);

}

// 清空

//stack.clear();

System.out.println("size = " + stack.size());

}

}

输出

1

1

true

0

10

10

9

8

7

6

5

4

3

2

1

size = 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值