데이터 구조 (양 끝 스 택)

42294 단어
2 단 스 택 의 순서 저장 구조: 2 단 스 택: 하나의 선형 표 의 2 단 을 스 택 밑 으로 각각 스 택 에 들 어가 고 스 택 에서 나 오 는 작업 Array StackDouble End < >:
public class ArrayStackDoubleEnd<E> implements Stack<E>{
	
	enum Direction{    //     
		LEFT,RIGHT;    
	}
	
	private E[] data;		//         
	private int leftTop;	//          -1
	private int rightTop;	//          data.length
	private static int DEFAULT_SIZE=10;	//       10
	
	public ArrayStackDoubleEnd(){
		this(DEFAULT_SIZE);
	}
	public ArrayStackDoubleEnd(int capacity){
		data=(E[]) new Object[capacity];
		leftTop=-1;		//            -1 
		rightTop=data.length;		//            data.length 
	}
	private boolean isFull(){
		return leftTop+1==rightTop;
	}
	/**
	 *           
	 * */
	public void push(Direction dir,E e){
		if(isFull()){		//      
			resize(data.length*2);	//      
		}
		if(dir==Direction.LEFT){
			data[++leftTop]=e;
		}else{
			data[--rightTop]=e;
		}
	}
	
	
	private void resize(int newLen) {		//        
		E[] newData=(E[]) new Object[newLen];
		//    
		for(int i=0;i<=leftTop;i++){
			newData[i]=data[i];
		}
		//    
		int index=data.length-1;
		int i;
		//6 7 8 9
		for(i=newData.length-1;i>=newData.length-data.length+rightTop;i--){
			newData[i]=data[index--];
		}
		rightTop=i+1;
		data=newData;
	}
	/**
	 *           
	 * */
	public E pop(Direction dir){
		if(dir==Direction.LEFT){
			if(leftTop==-1){
				throw new IllegalArgumentException("     !");
			}
			E e=data[leftTop--];
			if(getSize()<=data.length/4&&data.length>DEFAULT_SIZE){
				resize(data.length/2);
			}
			return e;
		}else{
			if(rightTop==data.length){
				throw new IllegalArgumentException("     !");
			}
			E e=data[rightTop++];
			if(getSize()<=data.length/4&&data.length>DEFAULT_SIZE){
				resize(data.length/2);
			}
			return e;
		}		
	}
	
	/**
	 *             
	 * */
	public E peek(Direction dir){
		if(dir==Direction.LEFT){
			if(leftTop==-1){
				throw new IllegalArgumentException("     !");
			}
			return data[leftTop];
		}else{
			if(rightTop==data.length){
				throw new IllegalArgumentException("     !");
			}
			return data[rightTop];
		}
	}
	
	/**
	 *             
	 * */
	public int getSize(Direction dir){
		if(dir==Direction.LEFT){
			return leftTop+1;
		}else{
			return data.length-rightTop;
		}
	}
	
	/**
	 *             
	 * */
	public boolean isEmpty(Direction dir){
		if(dir==Direction.LEFT){
			return leftTop==-1;
		}else{
			return rightTop==data.length;
		}
	}
	/**
	 *         
	 * */
	public void clear(Direction dir){
		if(dir==Direction.LEFT){
			leftTop=-1;
		}else{
			rightTop=data.length;
		}
	}
	
	/**
	 *               
	 * */
	@Override
	public int getSize() {
		return getSize(Direction.LEFT)+getSize(Direction.RIGHT);
	}
	
	/**
	 *               
	 * */
	@Override
	public boolean isEmpty() {
		return isEmpty(Direction.LEFT)&&isEmpty(Direction.RIGHT);
	}
	
	/**
	 *       
	 * */
	@Override
	public void push(E e) {
		if(getSize(Direction.LEFT)<=getSize(Direction.RIGHT)){
			push(Direction.LEFT,e);
		}else{
			push(Direction.RIGHT,e);
		}
	}
	
	/**
	 *       
	 * */
	@Override
	public E pop() {
		if(isEmpty()){
			throw new IllegalArgumentException("     !");
		}
		if(getSize(Direction.LEFT)>getSize(Direction.RIGHT)){
			return pop(Direction.LEFT);
		}else{
			return pop(Direction.RIGHT);
		}
	}
	
	/**
	 *              
	 * */
	@Override
	public E peek() {
		if(isEmpty()){
			throw new IllegalArgumentException("     !");
		}
		if(getSize(Direction.LEFT)>getSize(Direction.RIGHT)){
			return peek(Direction.LEFT);
		}else{
			return peek(Direction.RIGHT);
		}
	}
	
	/**
	 *        
	 * */
	@Override
	public void clear() {
		clear(Direction.LEFT);
		clear(Direction.RIGHT);
	}


toString () 방법 재 작성:
public String toString() {
		StringBuilder sb=new StringBuilder();
		sb.append("ArrayStackDoubleEnd: size="+getSize()+",capacity="+data.length+"
"
); if(isEmpty()){ sb.append("[]"); }else{ sb.append('['); int count=0; for(int i=0;i<=leftTop;i++){ sb.append(data[i]); count++; if(count==getSize()){ sb.append(']'); }else{ sb.append(','); } } for(int i=rightTop;i<data.length;i++){ sb.append(data[i]); count++; if(count==getSize()){ sb.append(']'); }else{ sb.append(','); } } } return sb.toString(); } //xxx.append toString

equal () 방법 재 작성:
public boolean equals(Object obj) {
		if(obj==null){
			return false;
		}
		if(obj==this){
			return true;
		}
		if(obj instanceof ArrayStackDoubleEnd){
			ArrayStackDoubleEnd<E> stack=(ArrayStackDoubleEnd) obj;
			if(getSize()==stack.getSize()){
				ArrayList<E> list1=new ArrayList<E>(getSize());
				ArrayList<E> list2=new ArrayList<E>(getSize());
				//         
				for(int i=0;i<=leftTop;i++){
					list1.addLast(data[i]);
				}
				//         
				for(int i=rightTop;i<data.length;i++){
					list1.addLast(data[i]);
				}
				//         
				for(int i=0;i<=stack.leftTop;i++){
					list2.addLast(stack.data[i]);
				}
				//         
				for(int i=stack.rightTop;i<stack.data.length;i++){
					list2.addLast(stack.data[i]);
				}
				return list1.equals(list2);
			}
		}
		return false;
	}

좋은 웹페이지 즐겨찾기