数据结构(双端栈)

双端栈的顺序存储结构:
双端栈:将一个线性表的俩端当做栈底分别进行入栈和出栈操作
ArrayStackDoubleEnd< >:

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+"\n");
		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;
	}

你可能感兴趣的:(数据结构(双端栈))