本程序只有入队,出队和判断队列为空的功能,希望对Java初学者有一定的帮助
import java.util.*;
class Queue
{
java.util.List
myList = new LinkedList();
// 入队列
public void enQueue(String s)
{
myList.add(s);
}
// 出队列
public String deQueue()
{
String s = myList.get(0);
myList.remove(0);
return s;
}
// 判断队列是否为空
public boolean isEmpty()
{
return myList.isEmpty();
}
}
public class Test
{
public static void main(String[] args)
{
Queue myQueue = new Queue();
myQueue.enQueue("How");
myQueue.enQueue("are");
myQueue.enQueue("you");
myQueue.enQueue("?");
while(!myQueue.isEmpty())
System.out.print(myQueue.deQueue() + " ");
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12921506/viewspace-217863/
,如需转载,请注明出处,否则将追究法律责任。