堆、队列、java实现

栈。

package com.zf.graph;

/**
 * 栈
 * @author zhoufeng
 * @param <T>
 */
public class StackX<T> {
	
	private Object[] array ;
	private int maxSize ;
	private int size  ;
	
	public StackX(int maxSize){
		this.maxSize = maxSize ;
		array = new Object[maxSize];
		size = 0 ;
	}
	
	public void push(T t){
		array[size++] = t ;
	}
	
	@SuppressWarnings("unchecked")
	public T peek(){
		return (T) array[size -1] ;
	}
	
	@SuppressWarnings("unchecked")
	public T pop(){   
		return (T) array[--size] ;
	}
	
	public boolean isEmpty(){
		return size == 0 ;
	}
	
	public boolean isFull(){
		return size == maxSize;
	}

}


队列

package com.zf.graph;


/**
 *	队列 
 * @author zhoufeng
 * @param <E>
 */
public class Queue<E> {

	private Object[] array  ;
	private int MAX_SIZE ;
	private int size ;
	private int head ;
	private int tail ;

	public Queue(int max_size){
		this.MAX_SIZE = max_size ;
		array = new Object[max_size];
		size = 0 ;
		head = 0 ;
		tail = -1 ;
	}

	public void push(E e){
		if(!isFull()){
			array[++tail] = e ;
			if(tail == MAX_SIZE - 1)
				tail = -1 ;
			size++;
		}else{
			throw new RuntimeException("添加失败,队列已满!");
		}
	}

	@SuppressWarnings("unchecked")
	public E peak(){
		if(!isEmpty()){
			return (E) array[head];
		}else{
			throw new RuntimeException("查看失败,队列已空!");
		}
	}

	@SuppressWarnings("unchecked")
	public E pop(){
		if(!isEmpty()){
			E top = (E) array[head++];
			if(head == MAX_SIZE) 
				head = 0 ;
			size--;
			return top;
		}else{
			throw new RuntimeException("移除失败,队列已空!");
		}
	}
	
	public boolean isEmpty(){
		return size == 0 ;
	}

	public boolean isFull(){
		return size == MAX_SIZE;
	}

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值