import java.util.NoSuchElementException;//通过数组来实现队列 public class ArrayQueue { //字段 public static Object[] data;//队列的元素个数 protected int size ;//队列头 protected int head;//队列尾 public static int tail;/ / //无参数构造函数 public ArrayQueue() { final int...
java 队列
//通过LinkedList实现队列
package 队列和堆栈;
import java.util.*;
public class LinkedListQueueTest {
//字段
private LinkedList list;
//无参数构造
public LinkedListQueueTest()
{
list=new LinkedList();
}
//队列元素的个数
public int size()
{
return list.size();
}
//进入队列
public void enqueue(Object obj)
{
list.addLast(obj);
}
//对头出来
public Object dequeue()
{
return list.removeFirst();
}
//浏览对头元素
public Object front()
{
//return list.getFirst();
return list.peekFirst();
}
//判断队列是否为空
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedListQueueTest llq=new LinkedListQueueTest();
System.out.println(llq.isEmpty());
llq.enqueue("147");
llq.enqueue("258");
llq.enqueue("369");
System.out.println(llq.size());
System.out.println("移除队列头元素:"+llq.dequeue());
System.out.println(llq.size());
llq.enqueue("abc");
llq.enqueue("def");
System.out.println(llq.size());
System.out.println("查看队列的头元素:"+llq.front());
System.out.println(llq.size());
System.out.println(llq.isEmpty());
}
}
通过数组实现
package 队列和堆栈;
import java.util.NoSuchElementException;
//通过数组来实现队列
public class ArrayQueue {
//字段
public static Object[] data;
//队列的元素个数
protected int size ;
//队列头
protected int head;
//队列尾
public static int tail;
/**
*
*/
//无参数构造函数
public ArrayQueue() {
final int INITIAL_LENGTH=3;
data=new Object[INITIAL_LENGTH];
size=0;
head=0;
tail=-1;
}
//队列元素个数方法
public int size()
{
return size;
}
public boolean isEmpty()
{
return size==0;
}
//得到队列头元素
public Object front()
{
if(size==0)
throw new NoSuchElementException();
return data[head];
}
//进入队列enqueue()方法
public void enqueue(Object obj)
{
//此时队列已经满
if(size==data.length){
Object[] oldData=data;
data=new Object[data.length*2];
//if(head==0)
System.arraycopy(oldData, head, data, 0, oldData.length-head);
if(head>0)
System.arraycopy(oldData, 0, data, head+1, tail+1);
head=0;
tail=oldData.length-1;
}
tail=(tail+1)%data.length;
size++;
data[tail]=obj;
}
//队列的元素出队
public Object dequeue()
{
if(size==0)
throw new NoSuchElementException();
Object ele=data[head];
//循环队列
head=(head+1)%data.length;
size--;
return ele;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
通过向量实现:
//通过向量实现栈
package 队列和堆栈;
import java.util.*;
public class VectorStackTest {
//字段
Vector v;
//构造函数
public VectorStackTest()
{
v=new Vector();
}
//元素的个数
public int size()
{
return v.size();
}
//是否为空
public boolean isEmpty()
{
return size()==0;
}
//进栈
public Object Push(Object obj)
{
v.addElement(obj);
return obj;
}
//出栈方法
public Object Pop()
{
int len=size();
Object obj=Peek();
v.removeElementAt(len-1);
return obj;
}
//查看栈顶元素
public Object Peek()
{
int len = size();
if (len == 0)
throw new EmptyStackException();
return v.elementAt(len - 1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
VectorStackTest vst=new VectorStackTest();
System.out.println("大小:"+vst.size());
vst.Push("123");
vst.Push("456");
vst.Push("789");
vst.Push("abc");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
vst.Push("def");
vst.Push("147");
System.out.println("大小:"+vst.size());
System.out.println("栈顶:"+vst.Peek());
System.out.println("出栈:"+vst.Pop());
System.out.println(vst.Peek());
vst.Push("def");
vst.Push("147");
System.out.println(vst.Pop());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Pop());
vst.Push("1aadf");
vst.Push("2dafad");
vst.Push("123789");
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println(vst.Pop());
System.out.println(vst.Peek());
System.out.println("------------------end------------");
VectorStackTest llst=new VectorStackTest();
llst.Push("123");
llst.Push("456");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.Peek());
llst.Push("789");
llst.Push("abc");
System.out.println("栈顶:"+llst.Peek());
System.out.println("出栈:"+llst.Pop());
System.out.println(llst.size());
System.out.println("栈顶:"+llst.Peek());
}
}
推荐:都看API文档。有疑问可以问我,QQ:2854791972010-08-08
java类库有Queue类,但是如果楼主想自己定义队列的话,可以模仿C++指针定义队列的方式。java和C#虽然没有指针,但是它们的对象默认都是传引用的,也就像指针传递地址一样,呵呵。2010-08-09
java中LinkedList实现了 Queue接口,也就是支持队列,你看jdk文档吧2010-08-09
肯定自带了,名字就叫Queue,看看java文档吧2010-08-08
查API文档。。。2010-08-08