设计数据结构SetOfStacks, 由多个栈组成,并且在前一个栈填满时新建一个栈

设想有一堆盘子,堆太高可能会倒下来。因此,现实生活中,盘子堆到一定高度时,我们就会另外堆一堆盘子。请事先设计数据结构SetOfStacks, 模拟这种行为。

SetOfStacks应该由多个栈组成,并且在前一个栈填满时新建一个栈。此外,SetOfStacks.push() 和SetOfStacks.pop()应该与普通栈的操作方法相同(也就是说,

pop()返回的值,应该跟只有一个栈时的情况一样)。

 

  1. import java.util.Stack;  
  2.   
  3.   
  4. public class SetOfStacks {  
  5.     class stack {  
  6.         public void push(int v);  
  7.         public int pop();  
  8.     }  
  9.       
  10.     //push and pop are both operated from the last stack, just pay attention if the last stack is empty or full  
  11.     public void push(int value) {  
  12.         Stack<E> last = getLastStack();  
  13.         if(last != null && !last.isFull()) {  
  14.             last.push(value);  
  15.         }  
  16.         else {  
  17.             Stack stack = new Stack(capacity);  
  18.             stack.push(value);  
  19.             stacks.add(stack);  
  20.         }  
  21.     }  
  22.       
  23.     public int pop() {  
  24.         Stack last = getLastStack();  
  25.         int v = last.pop();  
  26.         if (last.size == 0 ) {  
  27.             stacks.remove(stacks.size() -1);  
  28.         }  
  29.         return -1;  
  30.     }  
  31. }  

 

进阶:实现一个popAt( int index)方法,根据指定的子栈,执行pop操作。

  1. public class SetOfStacks{  
  2.   
  3.       ArrayList<MyStack2> stacks= new ArrayList<MyStack2>();  
  4.       int capacity,numOfStack;  
  5.       public static void main(String[] args) {  
  6.              // TODO Auto-generated method stub  
  7.   
  8.       }  
  9.   
  10.       public void push(int val){  
  11.             MyStack2 last=getLastStack();  
  12.              if( last!= null &&! last.isFull())  
  13.                    last.push( val);  
  14.              else{  
  15.                   MyStack2 stack= new MyStack2( capacity);  
  16.                    stack.push( val);  
  17.                    stacks.add( stack);  
  18.                    numOfStack++;  
  19.             }  
  20.       }  
  21.         
  22.       public int pop(){  
  23.             MyStack2 last=getLastStack();  
  24.              int value= last.pop();  
  25.              if( last. size==0)  
  26.                    stacks.remove( last); //移除  
  27.              return value;  
  28.       }  
  29.   
  30.       /** 
  31.        * 思路:从栈1弹出元素时,移出栈2的栈底元素压入栈1中,随后将栈3的栈底元素压入栈2的栈顶…… 
  32.        *          (假定出最后一个栈外,其余栈都是满的) 
  33.        * @param index 
  34.        * @return 
  35.        */  
  36.       public int popAt(int index){  
  37.              return leftShift( index, true);  
  38.       }  
  39.         
  40.       public int leftShift( int index, boolean removeTop){  
  41.             MyStack2 stack= stacks.get( index);  
  42.              int removedItem;  
  43.              if( removeTop)  
  44.                    removedItem= stack.pop();  
  45.              else  
  46.                    removedItem= stack.removeBottem();  
  47.               
  48.              if( stack.isEmpty()){  
  49.                    stacks.remove( stack);  
  50.             } else if(! stacks.get( index+1).isEmpty()){  
  51.                    int v=leftShift( index+1, false);  
  52.                    stack.push( v);  
  53.             }  
  54.              return removedItem;  
  55.       }  
  56.         
  57.       public MyStack2 getLastStack() {  
  58.              // TODO Auto-generated method stub  
  59.              return stacks.get( numOfStack-1);  
  60.       }  
  61. }  
  62.   
  63. class MyStack2{  
  64.       private int capacity;  
  65.       public Node top,bottem;  
  66.       public int size;  
  67.         
  68.       public MyStack2( int capacity){  
  69.              this. capacity= capacity;  
  70.       }  
  71.         
  72.       public boolean isFull(){  
  73.              return capacity== size;  
  74.       }  
  75.         
  76.       public void join(Node above,Node below){  
  77.              if( below!= null)  
  78.                    below. above= above;  
  79.              if( above!= null)  
  80.                    above. below= below;  
  81.       }  
  82.         
  83.       public boolean push(int value){  
  84.              if( size>= capacity)  
  85.                    return false;  
  86.              size++;  
  87.             Node n= new Node( value);  
  88.              if( size==1)  
  89.                    this. bottem= n;  
  90.             join( n, this. top);  
  91.              top= n;  
  92.               
  93.              return true;  
  94.       }  
  95.         
  96.       public int pop(){  
  97.             Node n= top;  
  98.              top= top. below;  
  99.              size--;  
  100.              return n. data;  
  101.       }  
  102.         
  103.       public boolean isEmpty(){  
  104.              return size==0;  
  105.       }  
  106.         
  107.       public int removeBottem(){  
  108.             Node b= bottem;  
  109.              bottem= bottem. above;  
  110.              if( bottem!= null)  
  111.                    bottem. below= null;  
  112.              size--;  
  113.              return b. data;  
  114.       }  
  115. }

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值