描述如何只用一个数组来实现三个栈

两种思路:固定分割和弹性分割

 

方法一:固定分割[java] view plain copy

 

  1. //固定分割  
  2.       int stackSize=100;  
  3.       int[] buffer=new int[3*stackSize];  
  4.       int[] stackPointer={-1,-1,-1};  
  5.         
  6.       public void push(int stackNum,int value) throws Exception{  
  7.              if( stackPointer[ stackNum]+1>= stackSize)  
  8.                    throw new Exception( "Out of space.");  
  9.              stackPointer[ stackNum]++;  
  10.              buffer[absTopOfStack( stackNum)]= value;  
  11.       }  
  12.         
  13.       public int pop(int stackNum) throws Exception{  
  14.              if( stackPointer[ stackNum]==-1)  
  15.                    throw new Exception( "Trying to pop an empty stack.");  
  16.              int value= buffer[absTopOfStack( stackNum)];  
  17.              buffer[absTopOfStack( stackNum)]=0;  
  18.              stackPointer[ stackNum]--;  
  19.              return value;  
  20.       }  
  21.         
  22.       public int peek(int stackNum){  
  23.              return buffer[absTopOfStack( stackNum)];  
  24.       }  
  25.         
  26.       public boolean isEmpty( int stackNum){  
  27.              return stackPointer[ stackNum]==-1;  
  28.       }  
  29.         
  30.       //返回栈stackNum栈顶元素的索引,绝对量  
  31.       public int absTopOfStack( int stackNum){  
  32.              return stackNum* stackSize+ stackPointer[ stackNum];  
  33.       }  

 

方法二:弹性分割[java] view plain copy

 

  1. //弹性分割  
  2. /** 
  3.  * 思路:当一个栈的元素个数超出其初始容量时,就将这个栈扩容至许可的容量,必要时还要搬移元素。 
  4.  *          将数组设计成环状,最后一个栈可能从数组末尾开始,环绕到数组开头。 
  5.  */  
  6. class StackData{  
  7.       int size=0;  
  8.       int start;  
  9.       int capacity=0;  
  10.       int pointer=0;  
  11.         
  12.       public StackData( int start, int capacity){  
  13.              this. start= start;  
  14.              this. capacity= capacity;  
  15.       }  
  16.         
  17.       public boolean isWithinStack( int index, int totalSize){  
  18.              if( start<= index&& index< start+ capacity)  
  19.                    return true;  
  20.              else if( start+ capacity> totalSize&& index<( start+ capacity)% totalSize)  
  21.                    return true;  
  22.              return false;  
  23.       }  
  24. }  
  25.   
  26. class SolutionB{  
  27.       static int numOfStack=3;  
  28.       static int defaultSize=4;  
  29.       static int totalSize=numOfStack*defaultSize;  
  30.       static StackData[] stacks={ new StackData(0, defaultSize), new StackData(defaultSize, defaultSize ),  
  31.                    new StackData( defaultSize*2, defaultSize)};  
  32.       static int[] buffer=new int[totalSize];  
  33.         
  34.       public static int numberOfElements(){  
  35.              return stacks[0]. size+ stacks[1]. size+ stacks[2]. size;  
  36.       }  
  37.         
  38.       public static int nextElements( int index){  
  39.              if( index+1>= totalSize)  
  40.                    return 0;  
  41.              else  
  42.                    return index+1;  
  43.       }  
  44.         
  45.       public static int previousElements( int index){  
  46.              if( index==0)  
  47.                    return totalSize-1;  
  48.              else  
  49.                    return index-1;  
  50.       }  
  51.         
  52.       //以相反顺序搬移元素  
  53.       public static void shift(int stackNum){  
  54.             StackData stack= stacks[ stackNum];  
  55.              if( stack. size>= stack. capacity){  
  56.                    int nextStack=( stackNum+1)% numOfStack;  
  57.                    shift(nextStack);  
  58.                    stack. capacity++;  
  59.             }  
  60.               
  61.              for( int i=( stack. size+ stack. capacity-1)% totalSize; stack.isWithinStack( i, totalSize);  
  62.                          previousElements(i)){  
  63.                    buffer[ i]= buffer[ previousElements(i)];  
  64.             }  
  65.              stack. start=0;  
  66.              stack. start= nextElements(stack.start);  
  67.              stack. pointer= nextElements(stack.start);  
  68.              stack. capacity--;  
  69.       }  
  70.         
  71.       /*搬移到其他栈上,以扩大容量*/  
  72.       public static void expand(int stackNum){  
  73.              shift((stackNum+1)%totalSize);  
  74.              stacks[ stackNum]. capacity++;  
  75.       }  
  76.         
  77.       public static void push(int stackNum,int value) throws Exception{  
  78.             StackData stack= stacks[ stackNum];  
  79.              //检查空间是否足够  
  80.              if( stack. size>= stack. capacity){  
  81.                    if( numberOfElements()>=totalSize)  
  82.                          throw new Exception( "Out fo space.");  
  83.                    else  
  84.                          expand(stackNum);  
  85.             }  
  86.               
  87.              stack. size++;  
  88.              stack. pointer= nextElements(stack.pointer);  
  89.              buffer[ stack. pointer]= value;  
  90.       }  
  91.         
  92.       public static int pop(int stackNum) throws Exception{  
  93.             StackData stack= stacks[ stackNum];  
  94.              if( stack. size==0)  
  95.                    throw new Exception( "Tryint to pop an empty stack.");  
  96.               
  97.              int value= buffer[ stack. pointer];  
  98.              buffer[ stack. pointer]=0;  
  99.              stack. pointer= previousElements(stack.pointer);  
  100.              stack. size--;  
  101.               
  102.              return value;  
  103.       }  
  104.         
  105.       public static int peek(int stackNum) throws Exception{  
  106.             StackData stack= stacks[ stackNum];  
  107.              return buffer[ stack. pointer];  
  108.       }  
  109.         
  110.       public static boolean isEmpty( int stackNum){  
  111.             StackData stack= stacks[ stackNum];  
  112.              return stack. size==0;  
  113.       }  
  114. }  
  115.    

转载于:https://my.oschina.net/u/2822116/blog/789374

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值