用java代码编写堆栈

public class Stack {
    int[] data;
    int maxSize;
    int top;

    public Stack(int maxSize) {
       this.maxSize = maxSize;
       data = new int[maxSize];
       top = -1;
    }

    /**

     * 依次加入数据
     * @param data 要加入的数据
     * @return 添加是否成功
     */
    public boolean push(int data) {
       if(top+1== maxSize) {
           System.out.println("栈已满!");
           return false;
       }
       this.data[++top] = data;
       return true;
    }
 
    /**
       * 从栈中取出数据
     * @return 取出的数据
     */
    public int pop() throws Exception{
       if(top==-1) {
           throw new Exception("栈已空!");
       }
       return this.data[top--];
    }

     public static void main(String[] args) throws Exception {
       Stack stack=new Stack(1000);
       stack.push(1);
       stack.push(2);
       stack.push(3);
       stack.push(4);
       stack.push(5);
       while(stack.top>=0)
       {
           System.out.println(stack.pop());
       }     
    }
}

 

下面是用Java编写堆栈类以及相关方法的测试用例: ```java import java.util.EmptyStackException; public class Stack { private int maxSize; private int top; private int[] stackArray; public Stack(int s) { maxSize = s; stackArray = new int[maxSize]; top = -1; } public void push(int j) { if (isFull()) { System.out.println("Stack is full."); } else { stackArray[++top] = j; } } public int pop() { if (isEmpty()) { throw new EmptyStackException(); } else { return stackArray[top--]; } } public int peek() { if (isEmpty()) { throw new EmptyStackException(); } else { return stackArray[top]; } } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } public void clear() { top = -1; } } ``` 测试用例代码如下: ```java import org.junit.Assert; import org.junit.Test; public class StackTest { @Test public void testPush() { Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); Assert.assertEquals(stack.peek(), 3); } @Test public void testPop() { Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); Assert.assertEquals(stack.pop(), 3); Assert.assertEquals(stack.pop(), 2); Assert.assertEquals(stack.pop(), 1); } @Test(expected = EmptyStackException.class) public void testEmptyPop() { Stack stack = new Stack(5); stack.pop(); } @Test(expected = EmptyStackException.class) public void testEmptyPeek() { Stack stack = new Stack(5); stack.peek(); } @Test public void testClear() { Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); stack.clear(); Assert.assertTrue(stack.isEmpty()); } @Test public void testIsFull() { Stack stack = new Stack(3); stack.push(1); stack.push(2); stack.push(3); Assert.assertTrue(stack.isFull()); } @Test public void testIsEmpty() { Stack stack = new Stack(3); Assert.assertTrue(stack.isEmpty()); stack.push(1); Assert.assertFalse(stack.isEmpty()); } } ``` 运行测试用例,查看是否能够通过测试: ``` JUnit version 4.12 ... Time: 0.007 OK (6 tests) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值