使用java实现堆栈功能

堆栈的原理

栈(stack)又名堆栈,一个数据集合,可以理解为只能在一端进行插入或删除操作的列表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。

栈就是一个桶,后放进去的先拿出来,它下面本来有的东西要等它出来之后才能出来(先进后出)

栈的基本操作:
  进栈(压栈):push
  出栈:pop

java实现堆栈

(1)通过List实现一个堆栈:

public class Stack_List {

	 public static void main(String[] args){  

	      
	        System.out.println("通过List实现一个堆栈:");
	        Stack stack = new Stack();  
	        stack.push(1);
	        stack.push(2);
	        stack.push(3);
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());
	        stack.push(4);	        
	        stack.push(5);
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());

	    }  
}

	//使用集合定义一个堆栈
	class Stack { 

	    List<Integer> list = new ArrayList<Integer>();  
	    int index = 0; //下标 

	    //入栈
	    public void push(int n){  
	        list.add(n);  
	        index++;  
	    } 

	    //出栈
	    public int pop(){  
	       if(!list.isEmpty()){  
	           index--;  
	           return list.remove(index);  
	       }  
	       return -1;  
	    }  
	}

(2)通过数组实现一个堆栈:

public class Stack_Array {

	 public static void main(String[] args){  

	        System.out.println("数组实现一个堆栈:");
	        Stack stack = new Stack();  
	        stack.push(1);
	        stack.push(2);
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());
	        stack.push(3);
	        stack.push(4);
	        stack.push(5);
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());
	        System.out.println(stack.pop());

	    }  
	}  

	//使用数组定义一个堆栈
	class Stack {  

	    int[] a = new int[5];  
	    int i = 0; //数组下标

	    //入栈 
	    public void push(int n){  
	      a[++i] = n; 
	    }  

	    //出栈
	    public int pop(){  
	      if(i>0){  
	         return a[i--];  
	      }
	      return -1;  
	    }  

}

(3)通过两个队列实现一个堆栈:

public class Stack_Queue {

	  public static void main(String[] args) {

	         System.out.println("两个队列实现一个堆栈:");
	         Stack stack = new Stack();
	         stack.push(1);
	         stack.push(2);
	         stack.push(3);
	         stack.push(4);
	         System.out.println(stack.pop());
	         System.out.println(stack.pop());
	         stack.push(5);
	         System.out.println(stack.pop());
	         System.out.println(stack.pop());
	         System.out.println(stack.pop());   

	    }

	}

	//两个队列实现一个堆栈
	class Stack {
	    Queue<Integer> queueA = new ArrayDeque<Integer>();
	    Queue<Integer> queueB = new ArrayDeque<Integer>();

	    //入栈
	    public void push(int n){
	        queueA.add(n);
	    }

	    //出栈
	    public int pop(){
	        //如果queueA为空,queueB有元素, 将queueB的元素依次放入queueA中,直到最后一个元素,我们弹出。
	        if(queueA.isEmpty()){
	            while (queueB.size() > 1) {
	                queueA.add(queueB.poll());//poll()移出并返回队列的头元素,如果队列为空,则返回null
	            }
	            return queueB.poll();
	        }
	        if(queueB.isEmpty()){
	            while (queueA.size() > 1) {
	                queueB.add(queueA.poll());
	            }
	            return queueA.poll();
	        }
	        return -1;
	    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值