3.1 题目:
使用一个数组,实现3个栈,每个栈大小固定
解法:
- package StackAndQueue;
- public class StackUsingArr_FixedSize {
- private int stackSize;
- private int[] buffer ;
- private int[] stackPointer;
- public StackUsingArr_FixedSize(int stackSize,int num) {
- this.stackSize = stackSize;
- this.buffer = new int[stackSize * num];
- this.stackPointer = new int[num];
- for(int i = 0;i<num;i++){
- stackPointer[i] = -1;
- }
- }
- public void push(int stackNum,int value) throws Exception{
- if(stackPointer[stackNum] + 1 >= stackSize){
- throw new Exception("Out of space");
- }
- stackPointer[stackNum]++;
- buffer[absTopOfStack(stackNum)] = value;
- }
- public int pop(int stackNum) throws Exception{
- if(stackPointer[stackNum] == -1){
- throw new Exception("Trying to pop an empty stack");
- }
- int value = buffer[absTopOfStack(stackNum)];
- buffer[absTopOfStack(stackNum)] = 0;
- stackPointer[stackNum]--;
- return value;
- }
- public int peek(int stackNum) throws Exception{
- if(isEmpty(stackNum)){
- throw new Exception("the stack is empty");
- }
- return buffer[absTopOfStack(stackNum)];
- }
- public boolean isEmpty(int stackNum) throws Exception{
- if(stackNum > stackPointer.length - 1)
- throw new Exception("Out of stack number index boundary");
- return stackPointer[stackNum] == -1;
- }
- private int absTopOfStack(int stackNum) throws Exception {
- if(stackNum > stackPointer.length - 1)
- throw new Exception("Out of stack number index boundary");
- return stackNum * stackSize + stackPointer[stackNum];
- }
- }
测试用例:
- @Test
- public void test_3_1() throws Exception {
- StackUsingArr_FixedSize stack = new StackUsingArr_FixedSize(100, 3);
- try {
- stack.pop(0);
- } catch (Exception e) {
- System.out.println("空栈");
- }
- for(int i=0;i<101;i++){
- try {
- stack.push(0, i);
- } catch (Exception e) {
- System.out.println("栈满了");
- }
- }
- try {
- stack.peek(2);
- } catch (Exception e) {
- System.out.println("空栈");
- }
- System.out.println(stack.peek(0));
- }
测试结果:
扫描二维码关注公众号,回复:
68518 查看本文章
空栈
栈满了
空栈
99