java 队列实现
2009-03-05 15:00
357 查看
队列其实 所指生活中排队的现象,去商场购物,付款时需要排队, 买饭时需要排队, 好多事情都是需要排队, 排在第一位的则先处理,结束后, 后面的人都像前移动一位,在开发中也有好多这样的事情需要处理,如文件的下载,短信的发送功能, 等这些都是需要队列方式实现。好了, 废话不多说, 详情见下面代码!
package com.fanzhang;
class Queue //队列类
{
private int maxSize; //队列长度,由构造函数初始化
private long[] queArray; // 队列
private int front; //队头
private int rear; //队尾
private int nItems; //元素的个数
//--------------------------------------------------------------
public Queue(int s) // 构造函数
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // 进队列
{
if(rear == maxSize-1) // 处理循环
rear = -1;
queArray[++rear] = j; // 队尾指针加1,把值j加入队尾
nItems++;
}
//--------------------------------------------------------------
public long remove() // 取得队列的队头元素。
{
long temp = queArray[front++]; // 取值和修改队头指针
if(front == maxSize) // 处理循环
front = 0;
nItems--;
return temp;
}
//--------------------------------------------------------------
public long peekFront() // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。
{
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // 判队列是否为空。若为空返回一个真值,否则返回一个假值。
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // 判队列是否已满。若已满返回一个真值,否则返回一个假值。
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // 返回队列的长度
{
return nItems;
}
//--------------------------------------------------------------
}
public class IntegerQueue
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // 队列有5个元素
theQueue.insert(10); // 添加4个元素
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); // 移除3个元素
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
theQueue.insert(50); // 添加4个元素
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() ) // 遍历队列并移除所有元素
{
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
}
}
package com.fanzhang;
class Queue //队列类
{
private int maxSize; //队列长度,由构造函数初始化
private long[] queArray; // 队列
private int front; //队头
private int rear; //队尾
private int nItems; //元素的个数
//--------------------------------------------------------------
public Queue(int s) // 构造函数
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // 进队列
{
if(rear == maxSize-1) // 处理循环
rear = -1;
queArray[++rear] = j; // 队尾指针加1,把值j加入队尾
nItems++;
}
//--------------------------------------------------------------
public long remove() // 取得队列的队头元素。
{
long temp = queArray[front++]; // 取值和修改队头指针
if(front == maxSize) // 处理循环
front = 0;
nItems--;
return temp;
}
//--------------------------------------------------------------
public long peekFront() // 取得队列的队头元素。该运算与 remove()不同,后者要修改队头元素指针。
{
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // 判队列是否为空。若为空返回一个真值,否则返回一个假值。
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // 判队列是否已满。若已满返回一个真值,否则返回一个假值。
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // 返回队列的长度
{
return nItems;
}
//--------------------------------------------------------------
}
public class IntegerQueue
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // 队列有5个元素
theQueue.insert(10); // 添加4个元素
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); // 移除3个元素
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
theQueue.insert(50); // 添加4个元素
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() ) // 遍历队列并移除所有元素
{
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
}
}
相关文章推荐
- 数据结构学习----链式队列(Java实现)
- Java关于队列的自我实现
- 数据结构(二):队列的Java实现
- 【剑指offer-java版】5、用两个栈实现队列
- 数组实现循环队列(Java)
- java编写简单消息队列,实现高德坐标变形服务
- 【数据结构+Java】Java实现简单队列、循环队列操作
- Java阻塞队列的实现
- Java数组实现队列
- java 实现数据结构之队列
- Java记录 -53- LinkedList实现队列
- 两个栈实现一个队列以及两个队列实现一个栈(Java)
- java实现优先级队列
- Java利用Redis实现消息队列的示例代码
- 到底什么是消息队列?Java中如何实现消息队列?
- java 用两个栈实现队列
- java数据结构:队列与实现
- 特殊线性表--队列在JAVA中的实现
- java实现队列
- 经典面试题:两个栈实现队列+两个队列实现栈----java