java数据结构--stack

本文介绍了三种不同的栈实现方式:使用链表、数组和Vector。每种实现都详细展示了其核心代码,包括基本操作如压栈、弹栈及检查栈是否为空等。
 

下面介绍三种实现stack的方法。尽管java中有stack类,但是已经被废弃了。自己动手实现!!

1.  用链表实现

package com.jzm.stackQueueTree;
public class  LinkedStack<T> {	
	private  Node topNode;  //引用链表中的第一个节点
	
	public LinkedStack(){	
          clear();
	}//end default constructor

	private  class  Node{		
		 private T data;   //栈的元素		 	
		 private Node next; //指向下一个节点的连接	
		 		 
		 private  Node(T data){
	  		 this.data = data;
	  		 next = null;			 
	  	 }//end constructor
		 
		 private  T getData(){			  
			  return  data;			 
		 }
	 
		  private Node getNextNode(){			 
			  return next;
		 }		 		 
	}
		
	private  boolean isEmpty(){		 
		  return  topNode == null;  
	 }
	 
	 private  void clear(){		
		  topNode = null;
	 }
	 
	 
	public  void  push(T  newEntry){				
		  Node  newNode = new Node(newEntry);
		  newNode.next = topNode;	    
		  topNode =   newNode;
	}
	 
	 public  T pop(){	 
		  T top = null;
		  if(!isEmpty()){			   
			  top =     topNode.getData();
			  topNode = topNode.getNextNode();			  
		  }		 
		  return  top;
	 }	  
	 
	 public void display(){		
		  Node point = topNode;
	      System.out.println("从顶端元素开始:"); 
		  while(point != null){
			 System.out.println(point.getData()); 
             point = point.next;			
		  }		  
	 }

	 
	 public static void main(String[] args) {
		    LinkedStack<Integer> linkedStack  = new LinkedStack<Integer>();
		    for (int i = 0; i < 100; i++){
		         linkedStack.push(i);        
		        if (i %2 ==0) {
				 linkedStack.pop();	
				}
			}	
		       linkedStack.display();
	 }
}


2. 用数组实现

 

package com.jzm.stackQueueTree;

public class ArrayStack<T> {
    	
		private T[]    a;                         //用于存放值的数组
		private int    topIndex;                   //栈的顶端索引
		private final  static  int  MAX = 500;      //默认栈大小
				
		    public ArrayStack(){	
		    	 clear();
				 a = (T[]) new Object [MAX];		 
			 }	
		    		
		    public ArrayStack(int size){          //自定义栈大小 
		    	  clear();
				  a = (T[])new Object[size];		
			}

			private boolean isFull(){								
				return  (topIndex >= a.length-1);				
			}					
			
			public void clear(){              //清空栈				 topIndex = -1;
				 a = null;
			}		
			 
			public  void  push(T   newEntry){
				 if (!isFull()) {					
					  a[++topIndex] = newEntry;
				 }else {
					  
				      System.out.println("栈满了  topIndex="+topIndex);
				 }			
			}
			 					
		    public  T  pop(){	 
				  T  top = null;
				  if(topIndex >= 0){			   
					 top =  a[topIndex];
					 topIndex--;
				  }else{
					   System.out.println("栈为空");
				  }	 				 
				  return  top;
		    }	  
		    
		  public void display(){
			  System.out.println("ArrayStack从顶端元素开始:"); 
			  for(int i=topIndex; i>=0; i--){	    	   
		    	 System.out.println(a[i]); 
		      }		    	
		    }
		  
		  
	 public static void main(String[] args) {
		
		 ArrayStack<Integer> arrayStack  = new ArrayStack<Integer>();
		  for (int i = 0; i < 100; i++){			     
			      arrayStack.push(i);			      
		          if (i %2  == 0){	 	         
		        	 arrayStack.pop();
				  }
		          
			 } //end for
		           arrayStack.display(); 		  
		   }
	  	
}


3. 用vector实现

package com.jzm.stackQueueTree;
import java.util.Vector;

public class VectorStack<T>{
	private Vector <T>  stack;
	
	public VectorStack (){
		
		stack = new Vector<T>();
		
	}
	
    public VectorStack (int maxsize){
		
		stack = new Vector<T>(maxsize);
		
	}   
	 
	public  void  push(T   newEntry){
		
           stack.addElement(newEntry);
	}
		 					
    public  T  pop(){	   	
		  T  top = null;		  
		 if (!isEmpty()) {
			  top = stack.lastElement();
			  stack.removeElementAt(stack.size()-1);
		 }	  
		  return  top;
    }	 
    
    private  boolean isEmpty(){   	
        return stack.isEmpty();
    }
    
    public void display(){	   	
	    System.out.println("vectorStack从顶端元素开始:"); 	   
	    for (int i = stack.size()-1; i>=0; i--) {	   
	        System.out.println(stack.elementAt(i));
	    }
    }  
	
    public static void main(String[] args) {
		
		 VectorStack<Integer> vectorStack = new VectorStack<Integer>();
		
		 for (int i = 0; i < 100; i++){			     
			      
			      vectorStack.push(i);			      
		          if (i %2  == 0){	 	         
		        	  vectorStack.pop();
				  }		          
			 } //end for
		           vectorStack.display(); 		  
		   }  	
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值