描述如何只用一個數組來實現三個棧

兩種思路:固定分割和彈性分割java

 

方法一:固定分割[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 copyapp

 

  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. }  
相關文章
相關標籤/搜索