栈。
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;
}
}