您的位置:首页 > 理论基础 > 数据结构算法

基于数组的循环队列和基于链表的队列

2017-04-08 00:20 483 查看
数据的插入和取出涉及对当前数据量nItems属性的修改,大量的插入、移除操作时,对nItems属性的操作可能影响性能,可参考不包含数据项个数属性的队列实现。

基于数组的循环队列

包含数据项个数属性

package datastructure.c4.queue.queuedef;

public 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;
nItems++;
}
public long remove(){
long temp=queArray[front++];
if(front==maxSize){
front=0;
}
nItems--;
return temp;
}
public long peekFront(){
return queArray[front];
}
public boolean isEmpty(){
return (nItems==0);
}
public boolean isFull(){
return (nItems==maxSize);
}
public int size(){
return nItems;
}
}

package datastructure.c4.queue.queuedef;

public class QueueApp {
public static void main(String[] args) {
Queue theQueue=new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);

while(!theQueue.isEmpty()){
long n = theQueue.remove();
System.out.print(n);
System.out.print("  ");
}
System.out.println("");
}
}


不包含数据项个数属性

package datastructure.c4.queue.nonitems;

public class QueueWithoutN {
private int maxSize;
private long[] queArray;
private int front;
/**
* rear作为写值的标志,插入时会在rear的下一个位置写入新值,
* 当rear=maxSize-1,也就是rear指向的数组的最后一个值,
* 要将rear设为-1,形成循环队列
*/
private int rear;
public QueueWithoutN(int s){
maxSize=s+1;
queArray=new long[maxSize];
front=0;
rear=-1;
}
/**
* 新值位于rear的后一个位置
* @param j
*/
public void insert(long j){
if(rear==maxSize-1){
rear=-1;
}
queArray[++rear]=j;
}
/**
* 取值时取front位置当前值,front后移一位
* 当front是数组最后一个元素时,后移一位后front=maxSize,
* 令front=0
* @return
*/
public long remove(){
long temp=queArray[front++];
if(front==maxSize){
front=0;
}
return temp;
}
public long peek(){
return queArray[front];
}
/**
* 1、rear+1==front说明下次写入值会覆盖front当前值,说明队列为空
* 2、front+maxSize-1==rear --> rear-front=maxSize-1(数组下标最大值),
* 只能rear=maxSize-1,front=0(front是数组第一个元素,rear是数组最后一个元素),
* 下次写入也会覆盖front当前值
* @return front与rear位置差1的所有情况返回true
*/
public boolean isEmpty(){
return (rear+1==front || front+maxSize-1==rear);
}
/**
* 队列为空时rear位于front前一个位置,
* 写满时,rear位于front前两个位置
* @return front与rear位置差2的所有情况返回true
*/
public boolean isFull(){
return (rear+2==front || front+maxSize-2==rear);
}
/**
* 1、rear>=front,说明未循环,元素数量为rear与front之间的元素数
* 2、rear<front,说明已循环插入,(maxSize-front)为front到数组最后的元素数,
* (rear+1)为数组开始(下标为0)到rear间的元素数
* @return
*/
public int size(){
if(rear>=front){
return rear-front+1;
}else{
return (maxSize-front)+(rear+1);
}
}
}

基于双端链表的队列

package datastructure.c5.linklist.linkqueue;

public class Link {
public long dData;
public Link next;
public Link(long d){
dData=d;
}
public void displayLink(){
System.out.print(dData+" ");
}
}

package datastructure.c5.linklist.linkqueue;

public class FirstLastList {
private Link first;
private Link last;
public FirstLastList(){
first=null;
last=null;
}
public boolean isEmpty(){
return first==null;
}
public void insertLast(long dd){
Link newLink =new Link(dd);
if(isEmpty()){
first=newLink;
}else{
last.next=newLink;
}
last=newLink;
}
public long deleteFirst(){
long temp=first.dData;
if(first.next==null){
last=null;
}
first=first.next;
return temp;
}
public void displayList(){
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;
}
System.out.println("");
}
}

package datastructure.c5.linklist.linkqueue;

public class LinkQueue {
private FirstLastList theList;
public LinkQueue(){
theList=new FirstLastList();
}
public boolean isEmpty(){
return theList.isEmpty();
}
public void insert(long j){
theList.insertLast(j);
}
public long remove(){
return theList.deleteFirst();
}
public void displayQueue(){
System.out.print("Queue (front-->rear):");
theList.displayList();
}
}

package datastructure.c5.linklist.linkqueue;

public class LinkQueueApp {
public static void main(String[] args) {
LinkQueue theQueue=new LinkQueue();
theQueue.insert(20);
theQueue.insert(40);

theQueue.displayQueue();

theQueue.insert(60);
theQueue.insert(80);

theQueue.displayQueue();

theQueue.remove();
theQueue.remove();

theQueue.displayQueue();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 队列 链表