package graph;
/**
*
* 用作无向不带权图深度优先搜索用得栈
* @version [版本号, 2012-12-25]
*/
public class Stack
{
/**
* 栈中得数据,顶点的索引
*/
private int[] data;
/**
* 栈中的元素个数
*/
private int nElement;
/**
* <默认构造函数>
*/
public Stack()
{
this(20);
}
/**
* <默认构造函数>
*/
public Stack(int capacity)
{
data = new int[capacity];
nElement = 0;
}
/**
* 判断栈是否为空
* @return
* @see [类、类#方法、类#成员]
*/
public boolean isEmpty()
{
return nElement == 0;
}
/**
* 入栈操作,如果栈满,扩容一倍
* @param index
* @see [类、类#方法、类#成员]
*/
public void push(int index)
{
if (nElement == data.length)
{
int[] temp = new int[data.length * 2];
System.arraycopy(data, 0, temp, 0, data.length);
data = temp;
}
data[nElement++] = index;
}
/**
* 出栈操作
* @return
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public int pop()
throws Exception
{
if (isEmpty())
{
throw new Exception("Empty Stack Exception");
}
return data[--nElement];
}
/**
* 查看栈顶元素操作
* @return
* @see [类、类#方法、类#成员]
*/
public int peek()
{
return data[nElement - 1];
}
}
/*
* 文 件 名: Queue.java
* 修改时间: 2012-12-25
*/
package graph;
/**
* 用作无向不带权图广度优先搜索用得循环队列队列
* @version [版本号, 2012-12-25]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class Queue
{
/**
* 队列中得数据,顶点的索引
*/
private int[] data;
/**
* 队列中的元素个数
*/
private int nElement;
/**
* 队首和队尾指针
*/
private int head, tail;
/**
* <默认构造函数>
*/
public Queue()
{
this(20);
}
/**
* <默认构造函数>
*/
public Queue(int capacity)
{
if (capacity <= 0)
{
capacity = 20;
}
data = new int[capacity];
nElement = 0;
head = tail = -1;
}
/**
* 判断队列是否为空
* @return
* @see [类、类#方法、类#成员]
*/
public boolean isEmpty()
{
return nElement == 0;
}
/**
* 入列操作,如果队列满,扩容一倍
* @param index
* @see [类、类#方法、类#成员]
*/
public void enQueue(int index)
{
//空
if (isEmpty())
{
data[nElement++] = index;
head = tail = 0;
}
//满
else if (nElement == data.length)
{
int[] temp = new int[data.length * 2];
for (int i = 0, current = head; i < nElement; i++, current++)
{
if (current >= nElement)
{
current -= nElement;
}
temp[i] = data[current];
}
data = temp;
data[nElement++] = index;
head = 0;
tail = nElement - 1;
}
//正常
else
{
if (tail + 1 >= data.length)
{
tail -= data.length;
}
data[++tail] = index;
nElement++;
}
}
/**
* 出列操作
* @return
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public int deQueue()
throws Exception
{
if (isEmpty())
{
throw new Exception("Empty Queue Exception");
}
else
{
if (head + 1 >= data.length)
{
head -= data.length;
}
nElement--;
return data[head++];
}
}
/**
* 查看队首元素操作
* @return
* @see [类、类#方法、类#成员]
*/
public int peek()
{
return data[head];
}
}