用java实现循环队列?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java实现循环队列?相关的知识,希望对你有一定的参考价值。
实现进队和出队就行了,最好有全部的代码,如果太麻烦,写思路也行,谢谢
参考技术A 简单写了下,希望你能看明白import java.util.ArrayList;
public class SeqQueue
ArrayList<String> list;
public SeqQueue()
list = new ArrayList<String>();
public String getFirst()
if (!list.isEmpty())
String s = list.get(0);
list.remove(0);
return s;
return null;
public void insertLast(String s)
list.add(s);
public static void main(String[] args)
SeqQueue seq = new SeqQueue();
seq.insertLast("111");
seq.insertLast("222");
seq.insertLast("333");
System.out.println(seq.getFirst());
System.out.println(seq.getFirst());
System.out.println(seq.getFirst());
参考技术B class Element
int id;
String name;
Element(int a,String n)
id=a;name=n;
class SeqQueue
int first,last,maxsize;
Element queue[];
SeqQueue(int i)
maxsize=i;
first=last=-1;
queue=new Element[i];
public void clear()//置空
first=last=-1;
public boolean isEmpty()//判空
if(first==-1)return true;
else return false;
public Element getFirst()//取队列头元素
if(first==-1)return null;
else return queue[first+1];
public boolean isFull()//判满
if((last+1)%maxsize==first)return true;
else return false;
public boolean enQueue(Element e)//入队
if(this.isFull())return false;
if(this.isEmpty())
first=last=0;
else
last=(last+1)%maxsize;
queue[last]=e;
return true;
public Element deQueue()//出队
Element t=queue[first];
if(this.isEmpty())return null;
if(first==last)
queue[first]=null;
this.clear();
return t;
queue[first]=null;
first=(first+1)%maxsize;
return t;
public int getLength()//队列长度
if(last>=first)return last-first+1;
else return maxsize-(first-last)+1;
public void display()//打印所有元素
int i,j;
for (i=first,j=0;j<this.getLength();i=(i+1)%maxsize,j++)
System.out.println(queue[i].id);
本回答被提问者采纳 参考技术C 你有数据结构这本书吗。自己慢慢看去。上面有的。一模一样的原题。
Java用数组实现循环队列
复习了下数据结构,用Java的数组实现一下循环队列。
队列的类
1 //循环队列 2 class CirQueue{ 3 private int QueueSize; 4 private int front; 5 private int rear; 6 private int[] queueList ; 7 8 public CirQueue(int QueueSize){ 9 this.QueueSize = QueueSize; 10 queueList = new int[QueueSize]; 11 front = 0; 12 rear = 0; 13 } 14 15 //获取队列头元素 16 public int getQueueElement(){ 17 //如果队列不为空,返回队头元素,否则抛出异常提示队列为空 18 int element = -1; 19 if(!isEmpty()){ 20 element = queueList[front]; 21 return element; 22 } 23 else { 24 System.out.println("队列为空"); 25 return -1; 26 } 27 28 } 29 30 //出队 31 public int deQueue(){ 32 int element = -1; 33 if(!isEmpty()){ 34 element = queueList[front]; 35 front =(front+1)%QueueSize; 36 return element; 37 } 38 else { 39 System.out.println("队列为空"); 40 return -1; 41 } 42 43 } 44 45 46 47 48 49 //入队 50 public void enQueue(int element){ 51 //如果队列未满,添加元素到队尾,否则提示队列已满 52 if(!isFull()){ 53 queueList[rear] = element ; 54 rear = (rear+1)%QueueSize; 55 56 } 57 else { 58 System.out.println("队列已满"); 59 } 60 } 61 62 //判断队列是否为空 63 public boolean isEmpty(){ 64 boolean b = false; 65 if(rear == front) 66 b = true; 67 return b; 68 } 69 70 71 //判断队列是否已满 72 public boolean isFull(){ 73 boolean b = false; 74 if((rear+1)%QueueSize == front) 75 b = true; 76 return b; 77 } 78 79 }
创建对象并测试
package com.test; import java.util.*; public class StructTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //新建并初始化存储空间为3的循环队列(方便判断队满条件,浪费一个数组空间) CirQueue cirQueue = new CirQueue(4); //入队3个元素 cirQueue.enQueue(1); cirQueue.enQueue(2); cirQueue.enQueue(3); //获取队头元素,获取 但不改变队列 int temp = cirQueue.getQueueElement(); System.out.println(temp); //出队 获取队头元素,并且队头指针往后移一位 temp = cirQueue.deQueue(); System.out.println(temp); //再次获取队头元素 temp = cirQueue.getQueueElement(); System.out.println(temp); } }
输出:
1 1 2
以上是关于用java实现循环队列?的主要内容,如果未能解决你的问题,请参考以下文章