资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

循环队列出队java代码,循环队列入队代码

用java实现循环队列?

简单写了下,希望你能看明白

创新互联建站是少有的成都网站设计、成都网站制作、营销型企业网站、微信小程序定制开发、手机APP,开发、制作、设计、买友情链接、推广优化一站式服务网络公司,自2013年创立以来,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评

import java.util.ArrayList;

public class SeqQueue {

ArrayListString list;

public SeqQueue() {

list = new ArrayListString();

}

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());

}

}

循环队列代码求解释。。。

#includestdio.h//循环队列的存储结构

#includestdlib.h

#define OVERFLOW -2

#define maxqsize 100

typedef int QElemType;

typedef struct {

QElemType *base;

int front;

int rear;

}SqQueue;

void InitQueue(SqQueue *Q)

{//构造一个空队列Q

Q-base=(QElemType *)malloc(maxqsize * sizeof(QElemType));//申请空间

if(!Q-base) exit(OVERFLOW);//若没有申请到空间,则溢出出错

Q-front=Q-rear=0;

}

void ClearQueue(SqQueue *Q)

{//置空

Q-front=Q-rear;//将首尾指针指向同一处

}

void EnQueue(SqQueue *Q,QElemType e)

{//入队

printf("please input tne number you want input\n");

scanf("%d",e);

if((Q-rear+1)%maxqsize==Q-front) {printf("sorry The Queue is full!\nNOW The Program will be out!\n");exit(0);}//队尾指针已经指向循环队列的最大长度,则不能入队列

Q-base[Q-rear]=e;//将元素入队列

Q-rear=(Q-rear+1)%maxqsize;//改变队尾指针的值

printf("congratulation Enqueue successful\n");

}

void DeQueue(SqQueue *Q,QElemType e)

{//出队

if(Q-front==Q-rear) {printf("Sorry the queue is empty\nNOW The Program will be out!\n");exit(0);}//首尾指针指向同一处证明队列为空,没有元素可以退栈

e=Q-base[Q-front];//将队首元素出栈

Q-front=(Q-front+1)%maxqsize;//队首指针后移

printf("congratulation dequeue successful\n");

}

void QueueLength(SqQueue *Q)

{//求队列长度

printf("the length of the queue is %d\n",(Q-rear-Q-front+maxqsize)%maxqsize);

}

void PrintQueue(SqQueue *Q)

{//打印队列

int i=1;

if(Q-front==Q-rear) printf("Sorry The Queue is Empty\n");

while(Q-front!=Q-rear)//while(Q-front!=(Q-rear+1)%maxqsize)//以后者作为循环条件,用前者的话最后一个不能打印

{

printf("Queue the %dth is %d \n",i++,Q-base[Q-front]);

Q-front++;

}

}

void DestroyQueue(SqQueue *Q)

{//销毁队列

if(!Q-base) free(Q-base);

Q-base=NULL;

Q-rear=Q-front;

printf("DestroyQueue is OK!\nNOW The Program will be out!\n");

exit(0);

}

int main()

{

SqQueue Q;

QElemType e;

int i;

InitQueue(Q);

printf("please input what you want to do\nfor 0:over\n1:clearQueue\n2:EnQueue\n3:DeQueue\n4:find Queue length\n5:print Queue\n");

scanf("%d",i);

while(i)

{

switch(i)

{

case 1:ClearQueue(Q);break;

case 2:EnQueue(Q,e);break;

case 3:DeQueue(Q,e);break;

case 4:QueueLength(Q);break;

case 5:PrintQueue(Q);break;

}

printf("you can input aagain :\nfor 0:over\n1:clearQueue\n2:EnQueue\n3:DeQueue\n4:find Queue length\n5:print Queue\n");

scanf("%d",i);

}

return 0;

}

Java数据结构中,如何遍历输出循环队列中的元素?求代码~

你说的不太清楚, 遍历队列是数组  ,集合 ,map还是其他什么

我就写个数组的给你, 你要其他的话说明白了再写

public class Test {

public static void main(String[] args) {

// 定义数组

int[] a = new int[5];

// 初始化数组

for(int i = 0; i  5; i++){

a[i] = i ;

}

// 循环输出数组

for(int i = 0; i  5; i++){

System.out.print(a[i] + "  ");;

}

}

}

Java如何使用数组实现循环队列的案例

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;jthis.getLength();i=(i+1)%maxsize,j++)

System.out.println(queue[i].id);

}

}

java中,实现一个循环队列,其中的边界条件有些弄不明白,请看我的代码:

//我做了一个测试类,你运行一下试试吧

//问题的关键在于这个类的设计似乎是,假设size是3,但是数组的size是4

//putloc是0,但是put的位置在数组中是1

//总觉得这个类的设计很怪,既然size是3,底层实现也做成3就好了。

import java.util.Arrays;

public class CircularQueue {

private char q[];

private int putloc, getloc;

public static void main(String[] args) {

CircularQueue circularQueue = new CircularQueue(3);

circularQueue.put('1');

circularQueue.put('1');

circularQueue.put('1');

circularQueue.put('1');

}

private void paint(String s) {

System.out.println(s + ": putloc=" + putloc + " getloc=" + getloc + " "

+ Arrays.toString(q));

}

public CircularQueue(int size) {

q = new char[size + 1];// 注意:这里数组长度加 1。

putloc = getloc = 0;

paint("create!");

System.out.println();

}

public void put(char ch) {

paint("before put");

if (putloc + 1 == getloc | ((putloc == q.length - 1) (getloc == 0))) { // 第一个边界条件想不清楚,为什么putloc+1==getloc

System.out.println("--Queue is full.");

return;

}

putloc++;

if (putloc == q.length)

putloc = 0;

q[putloc] = ch;

paint("after put");

System.out.println();

}

public char get() {

paint("before get");

if (getloc == putloc) {

System.out.println("--Queue is empty.");

return (char) 0;

}

getloc++;

if (getloc == q.length)

getloc = 0;

paint("after get");

System.out.println();

return q[getloc];

}

}


网站题目:循环队列出队java代码,循环队列入队代码
当前URL:http://cdkjz.cn/article/hogcso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220