括号匹配、进制转换和堆栈

Stack接口:

package stack;

public interface Stack {
	//返回堆栈的大小
	public int getSize();
	//判断堆栈是否为空
	public boolean isEmpty();
	//数据元素e入栈
	public void push(Object e);
	//栈顶元素出栈
	public Object pop()throws StackEmptyException;
	//取栈顶元素看看
	public Object peek()throws StackEmptyException;
}

定义栈异常:

package stack;

public class StackEmptyException extends Exception{
	
	public StackEmptyException(String exception)
	{
		super(exception);
		
	}

}




节点:

package stack;

public class SLNode {
	public SLNode next;
	public Object e;
	
	public SLNode(SLNode next,Object e)
	{
		this.next=next;
		this.e=e;
	}

}

栈实现(链表)

package stack;

public class StackSLinked implements Stack{
	private SLNode top;
	private int size;//标记栈的大小
	
	public StackSLinked()
	{
		top=null;size=0;
	}

	@Override
	public int getSize() {
		
		return size;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return size==0;
	}

	@Override
	public void push(Object e) {
		SLNode q=new SLNode(top,e);
		top=q;
		size++;
		
	}

	@Override
	public Object pop() throws StackEmptyException {
		if(size<1)
			throw new StackEmptyException("栈为空");
		Object obj=top.e;
		top=top.next;
		size--;
		return obj;
	}

	@Override
	public Object peek() throws StackEmptyException {
		if(size<1)
		{
			throw new StackEmptyException("栈为空");
			
		}
		
		return top.e;
	}

}

Client 包含 括号匹配算法和


你可能感兴趣的:(进制转换)