程序员面试金典--题目解析-3.1 使用一个数组实现三个栈

3.1 题目:

使用一个数组,实现3个栈,每个栈大小固定


解法:

  1. package StackAndQueue;  
  2.   
  3. public class StackUsingArr_FixedSize {  
  4.     private int stackSize;  
  5.     private int[] buffer ;  
  6.     private int[] stackPointer;  
  7.       
  8.     public StackUsingArr_FixedSize(int stackSize,int num) {  
  9.         this.stackSize = stackSize;  
  10.         this.buffer = new int[stackSize * num];  
  11.         this.stackPointer = new int[num];  
  12.           
  13.         for(int i = 0;i<num;i++){  
  14.             stackPointer[i] = -1;  
  15.         }  
  16.           
  17.     }  
  18.       
  19.     public void push(int stackNum,int value) throws Exception{  
  20.         if(stackPointer[stackNum] + 1 >= stackSize){  
  21.             throw new Exception("Out of space");  
  22.         }  
  23.           
  24.         stackPointer[stackNum]++;  
  25.         buffer[absTopOfStack(stackNum)] = value;  
  26.     }  
  27.   
  28.     public int pop(int stackNum) throws Exception{  
  29.         if(stackPointer[stackNum] == -1){  
  30.             throw new Exception("Trying to pop an empty stack");  
  31.         }  
  32.           
  33.         int value = buffer[absTopOfStack(stackNum)];  
  34.         buffer[absTopOfStack(stackNum)] = 0;  
  35.         stackPointer[stackNum]--;  
  36.         return value;  
  37.     }  
  38.       
  39.     public int peek(int stackNum) throws Exception{  
  40.         if(isEmpty(stackNum)){  
  41.             throw new Exception("the stack is empty");  
  42.         }  
  43.           
  44.         return buffer[absTopOfStack(stackNum)];  
  45.     }  
  46.       
  47.     public boolean isEmpty(int stackNum) throws Exception{  
  48.         if(stackNum > stackPointer.length - 1)  
  49.             throw new Exception("Out of stack number index boundary");  
  50.           
  51.         return stackPointer[stackNum] == -1;  
  52.     }  
  53.       
  54.     private int absTopOfStack(int stackNum) throws Exception {  
  55.         if(stackNum > stackPointer.length - 1)  
  56.             throw new Exception("Out of stack number index boundary");  
  57.           
  58.         return stackNum * stackSize + stackPointer[stackNum];  
  59.     }  
  60.       
  61. }  



测试用例:

  1. @Test  
  2.     public void test_3_1() throws Exception {  
  3.         StackUsingArr_FixedSize stack = new StackUsingArr_FixedSize(1003);  
  4.         try {  
  5.             stack.pop(0);  
  6.         } catch (Exception e) {  
  7.             System.out.println("空栈");  
  8.         }  
  9.         for(int i=0;i<101;i++){  
  10.             try {  
  11.                 stack.push(0, i);  
  12.             } catch (Exception e) {  
  13.                 System.out.println("栈满了");  
  14.             }  
  15.         }  
  16.           
  17.         try {  
  18.             stack.peek(2);  
  19.               
  20.         } catch (Exception e) {  
  21.             System.out.println("空栈");  
  22.         }  
  23.         System.out.println(stack.peek(0));  
  24.     }  


测试结果:

扫描二维码关注公众号,回复: 68518 查看本文章

空栈
栈满了
空栈

99


猜你喜欢

转载自blog.csdn.net/kingmore96/article/details/80089046