简介
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表。
队列空的条件: front=rear
队列满的条件: rear = MAXSIZE
/**
* simulate queue struct by java
*
* @author Bruce
* @date Sep 8, 2011
* @version 1.0
*/
public class Queue {
private int maxSize;
private long[] queueArray;
private int front;// the head of queue
private int rear;// the end of queue
private int nItems;
/**
* Constructor a queue,the size is s;now the queue is null, and the front is
* zero and the rear is negative one
*
* @param s
*/
public Queue(int s) {
maxSize = s;
queueArray = new long[maxSize];
front = 0;
rear = 0;
nItems = 0;
}
public void insert(long j) {
// if the the inserted items great the size of queue,
// the rear return the initial value and the next item will
// override the corresponding value
if (rear == maxSize)
rear = 0;
queueArray[rear++] = j;
nItems++;// the count of the items
}
/**
* remove the item from the 'front' position,every time the front will be
* added one
*/
public long remove() {
long temp = queueArray[front++];
// if the front equals the size of queue,the front will return the
// initial value
if (front == maxSize)
front = 0;
nItems--;
if (nItems < 0)
nItems = 0;
return temp;
}
public long peekFront() {
return queueArray[front];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
public int size() {
return nItems;
}
public static void main(String[] args) {
Queue queue = new Queue(3);
queue.insert(3);
queue.insert(6);
queue.insert(8);
// queue.insert(4);
System.out.println("size:" + queue.size());
System.out.println(queue.peekFront());
queue.remove();
System.out.println(queue.peekFront());
queue.remove();
System.out.println(queue.peekFront());
queue.remove();
// System.out.println(queue.peekFront());
// queue.remove();
System.out.println("size:" + queue.size());
}
}
分享到:
相关推荐
这是一个java队列实现的全部工程文件,包含了所有代码,具体的设计文档在上传的另外文件中。这个工程能够实现所有队列的操作,运行没有问题。设计的是在应用程序上的基于界面的队列操作的实现。
这是模拟队列的实现,没有源码,源码在另外一个上传的文件中,基本实现了队列的各种操作。
实现java模拟阻塞队列的例子,该代码包括,阻塞队列实现生产者,消费者。和模拟阻塞队列实现生产者及消费者模式,帮助你更好的理解java多线程
这是一篇使用java模拟队列实现的程序的课程设计,包含了队列界面的设计以及队列数据结构的模拟实现,是java学习的很好的参照,另外,包含了部分的源代码,具体源代码在其它文件夹。
java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号 java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号
NULL 博文链接:https://128kj.iteye.com/blog/1665616
通过2个栈 模拟 队列。
银行业务队列简单模拟 java银行业务队列简单模拟.zipjava银行业务队列简单模拟.zipjava银行业务队列简单模拟.zipjava银行业务队列简单模拟.zipjava银行业务队列简单模拟.zip
银行业务队列简单模拟 java银行业务队列简单模拟源码.zipjava银行业务队列简单模拟源码.zipjava银行业务队列简单模拟源码.zipjava银行业务队列简单模拟源码.zip
试用java.util.Stack泛型栈作为父类,用另一个泛型栈对象作为成员变量,模拟实现一个泛型子类Queue,当存储元素的第1个栈的元素超过dump时,再有元素入队列就倒入第2栈。除提供无参构造函数Queue( )外,其它所有队列...
一个模拟银行和超市的业务-顾客队列服务模拟系统,用了Java的多任务和多线程-a simulation of banks and supermarkets in the business-customer service cohort simulation system, using a Java multi-tasking and ...
Java模拟栈和队列数据结构的基本示例讲解共4页.pdf.zip
模拟操作系统对进程的调度 JAVA源代码
一个小煤矿用六辆自卸卡车将煤从出口处运到铁路。煤矿拥有两台装煤设备(Loaders)和一台称重机器(Scale),卡车在煤矿出口处装满煤,称重后运到铁路,卸车后再次回到煤矿。
java 队列使用,次例子是一个模拟网络爬虫工作大致流程的小例子,里面没有具体的爬取的实现,只是对爬取的流程的模拟,使用到了java 的 ArrayBlockingQueue、ConcurrentHashMap、 这2个类和java 的 volatile 关键字...
银行业务队列简单模拟
队列在计算机术语中使用queue,和list(排)其实是一个意思。队列也是一种数据结构,类似于栈,只是与栈相反,在队列中先插入的数据也先被移除,即先进先出(FIFO,First In First Out)。队列可以理解成排队,比如,...
模拟顾客排队队列,如银行、医院、超市等地,柜台数量以及顾客等待人数。eclipse开发,经检测,可运行。
idea+java多线程模拟队列实现排队叫号.zip