當前位置:編程學習大全網 - 源碼下載 - java 隊列

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:285479197

  • 上一篇:經典科幻電影 像加勒比海盜 黑衣人 戰艦之類的
  • 下一篇:微信如何建立網站?微信如何建立網站鏈接
  • copyright 2024編程學習大全網