http://yongsky.iteye.com/blog/128549
一、Java队列:
队列是设计程序中常用的一种数据结构。它类似日常生活中的排队现象,采用一种被称为“先进先出”(LIFO)的存储结构。数据元素只能从队尾进入,从队首取出。在队列中,数据元素的次序不会改变。每当有数据元素从队列中被取出,后面的数据元素依次向前移动一位。
LinkedList,即是数据结构中的Queue,内部实现是链表形式,队列主要的方法为:
1.插入:public boolean offer(E e)将指定元素添加到此列表的末尾(最后一个元素)
2.获取头元素,但不移除:public E peek()获取但不移除此列表的头(第一个元素)
3.获取头元素,而且移除:public E poll()获取并移除此列表的头(第一个元素)
4.判断是否为空:public boolean isEmpty()如果此 collection 不包含元素,则返回 true
二、用Vector快速实现JAVA的队列类
根据这些特点,对队列定义了以下六种操作:
enq(x) 向队列插入一个值为x的元素;
deq() 从队列删除一个元素;
front() 从队列中读一个元素,但队列保持不变;
empty() 判断队列是否为空,空则返回真;
clear() 清空队列;
search(x) 查找距队首最近的元素的位置,若不存在,返回-1。
Vector类是JAVA中专门负责处理对象元素有序存储和任意增删的类,因此,用Vector可以快速实现JAVA的队列类。
public class Queue extends java.util.Vector {
public Queue() {
super();
}
public synchronized void enq(Object x) {
super.addElement(x);
}
public synchronized Object deq() {
/* 队列若为空,引发EmptyQueueException异常 */
if( this.empty() )
throw new EmptyQueueException();
Object x = super.elementAt(0);
super.removeElementAt(0);
return x;
}
public synchronized Object front() {
if( this.empty() )
throw new EmptyQueueException();
return super.elementAt(0);
}
public boolean empty() {
return super.isEmpty();
}
public synchronized void clear() {
super.removeAllElements();
}
public int search(Object x) {
return super.indexOf(x);
}
}
public class EmptyQueueException extends java.lang.RuntimeException {
public EmptyQueueException() {
super();
}
}
三、栈与队列
(1)栈
package ChapterOne;
public class Stack {
//栈数组
long stackArr[];
//栈的大小
int maxSize;
//栈的顶部
int top;
//初始化一个大小为size的栈
public Stack(int size){
maxSize = size;
stackArr = new long[size];
top = -1;
}
//出栈操作
public long pop(){
return stackArr[top--];
}
//进栈操作
public void push(long value){
stackArr[++top] = value;
}
//判断栈是否为空
public boolean isEmpty(){
return top == -1;
}
//判断栈是否已满
public boolean isFull(){
return top == maxSize-1;
}
//取栈顶元素
public long peek(){
return stackArr[top];
}
public static void main(String[] args) {
Stack stack = new Stack(10);
while(!stack.isFull()){
long v = (long) (Math.random()*100);
stack.push(v);
System.out.print(v+" ");
}
System.out.println();
while(!stack.isEmpty()){
long topValue = stack.pop();
System.out.print(topValue+" ");
}
System.out.println();
}
}
(2)队列
package ChapterOne;
public class Queue {
//队列数组
private long queueArr[];
//队列的前端下标
private int front;
//队列的尾端下标
private int rear;
//队列的大小
private int maxSize;
//队列中元素的个数
private int nItems;
//初始化一个大小为size的队列
public Queue(int size){
queueArr = new long[size];
maxSize = size;
front = 0;
rear = -1;
nItems = 0;
}
//插入操作
public void insert(long value){
//队列已满
if(rear == maxSize-1)
rear = -1;
queueArr[++rear] = value;
nItems++;
}
//删除操作
public long remove(){
long temp = queueArr[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
//返回队列第一个元素
public long peakFront(){
return queueArr[front];
}
//判断是否为空
public boolean isEmpty(){
return nItems == 0;
}
//判断是否已满
public boolean isFull(){
return nItems == maxSize;
}
//返回队列中元素的个数
public int size(){
return nItems;
}
public void print(){
for(int i = front;i < front+nItems;i++){
System.out.print(queueArr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Queue q = new Queue(10);
while(!q.isFull()){
long value = (long)(Math.random()*100);
q.insert(value);
}
q.print();
while(!q.isEmpty()){
q.remove();
q.print();
}
q.print();
System.out.println(q.isEmpty());
}
}
(3)优先队列
package ChapterOne;
public class PriorityQueue {
private int nItems;
private long pqArr[];
private int maxSize;
public PriorityQueue(int size){
maxSize = size;
pqArr = new long[size];
nItems = 0;
}
public void insert(long value){
int i;
if(nItems == 0)
pqArr[nItems++] = value;
else{
for(i = nItems-1;i >= 0;i--){
if(value < pqArr[i]){
pqArr[i+1] = pqArr[i];
}
else
break;
}
pqArr[i+1] = value;
nItems++;
}
}
public long remove(){
return pqArr[--nItems];
}
public boolean isEmpty(){
return nItems == 0;
}
public boolean isFull(){
return nItems == maxSize;
}
public void print(){
for(int i = 0;i < nItems;i++)
System.out.print(pqArr[i]+" ");
System.out.println();
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue(10);
while(!pq.isFull()){
long value = (long)(Math.random()*100);
pq.insert(value);
}
pq.print();
}
}
栈与队列Queue
猜你喜欢
转载自lixg425.iteye.com/blog/1600567
今日推荐
周排行