队列:
含义:一个只能在队首进行删除,队尾进行插入的线性表,其特征是先进先出。
链式队列:
入队列:在对位插入
出队列:在对头删除
循环队列:
出现的原因是有可能发生满对列但是他又确实不是满的情况
package com.dataStructure.queue; import java.util.Arrays; public class MyQueue { private static int DEFAULT_SIZE = 10; private int capacity; private String[] datas; private int front; private int rear; //default array length to create empty queue public MyQueue() { this.capacity = DEFAULT_SIZE; this.datas = new String[this.capacity]; } //create a queue by a initial element public MyQueue(String dataItem) { this(); this.datas[0] = dataItem; this.rear++; } //create a queue by specified length and an dataItem public MyQueue(String dataItem, int specifiedLength) { this.capacity = specifiedLength; this.datas = new String[this.capacity]; this.datas[0] = dataItem; this.rear++; } //Get Queue Size public int getQueueLength(){ return this.rear - this.front; } //Check queue is empty public boolean isEmpty(){ return this.rear == this.front; } //Insert data public void insertData(String data){ if(this.rear > this.capacity -1){ return; } this.datas[this.rear++] = data; } //Remove from queue public String removeData(){ if(isEmpty()) return null; String oldData = this.datas[this.front]; this.datas[this.front] = null; this.front++; return oldData; } public String element(){ if(isEmpty()){ return null; } return this.datas[this.front]; } public void clear(){ Arrays.fill(this.datas,null); front = 0; rear = 0; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public String[] getDatas() { return datas; } public void setDatas(String[] datas) { this.datas = datas; } public int getFront() { return front; } public void setFront(int front) { this.front = front; } public int getRear() { return rear; } public void setRear(int rear) { this.rear = rear; } }
package com.dataStructure.queue; /** * 循环队列出现的原因是有可能发生满对列但是他又确实不是满的情况 * 从而为了避免这样的情况发生,导致数据不能插入 * @author Administrator * */ public class LoopQueue { private static int DEFAULT_SIZE = 10; private int capacity; private String[] datas; private int front; private int rear; //default array length to create empty queue public LoopQueue() { this.capacity = DEFAULT_SIZE; this.datas = new String[this.capacity]; } //create a queue by a initial element public LoopQueue(String dataItem) { this(); this.datas[0] = dataItem; this.rear++; } //create a queue by specified length and an dataItem public LoopQueue(String dataItem, int specifiedLength) { this.capacity = specifiedLength; this.datas = new String[this.capacity]; this.datas[0] = dataItem; this.rear++; } //Check queue is empty public boolean isEmpty(){ return this.rear == this.front && getDatas()[this.rear] == null; } //Get Queue Size public int getQueueLength(){ if(isEmpty()) return 0; return this.rear > this.front ? this.rear - this.front : this.capacity-(this.front-this.rear); } //Insert data public void insertData(String data){ if(this.rear > this.capacity -1 && this.datas[0] != null){ System.out.println("队列已满"); return; }else if(this.rear == this.front && this.datas[this.front] != null){ System.out.println("队列已满"); return; }else if(this.rear > this.capacity -1 && this.datas[0] == null){ this.rear = 0; } this.datas[this.rear] = data; rear++; } //Remove from queue public String removeData(){ if(isEmpty()) return null; String oldData = this.datas[this.front]; this.datas[this.front] = null; this.front++; return oldData; } public String element(){ if(isEmpty()){ return null; } return this.datas[this.front]; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public String[] getDatas() { return datas; } public void setDatas(String[] datas) { this.datas = datas; } public int getFront() { return front; } public void setFront(int front) { this.front = front; } public int getRear() { return rear; } public void setRear(int rear) { this.rear = rear; } }
package com.dataStructure.queue; public class Test { public static void main(String[] args) { // MyQueue queue = new MyQueue(); LoopQueue queue = new LoopQueue(); String book = "Java"; String star = "Briteny Spears"; String country = "China"; String state = "Califonia"; String name = "Tony"; String name1 = "Tony1"; String name2 = "Tony2"; String name3 = "Tony3"; String name4 = "Tony4"; String name5 = "Tony5"; String name6 = "Tony6"; queue.insertData(book); queue.insertData(star); queue.insertData(country); queue.insertData(state); queue.insertData(name); queue.insertData(name1); queue.insertData(name2); queue.insertData(name3); queue.insertData(name4); queue.insertData(name5); queue.removeData(); queue.removeData(); queue.insertData(name6); queue.insertData(star); queue.removeData(); queue.insertData(star); queue.insertData(star); System.out.println("Fornt :"+queue.getFront()); System.out.println("Rear :"+queue.getRear()); System.out.println(queue.getQueueLength()); } }
相关推荐
没有任何错误的。里面运用了队列和较为复杂的位运算来解决问题,用二进制来表示是否过河,不过本程序还有许多可以完善的地方
数据结构-队列的定义及基本操作(代码+报告)
数据结构-队列实验报告 本实验报告的主要目标是设计和实现一个队列数据结构,包括链队列、顺序队列和循环队列三个部分。通过对每种队列的设计和实现,学生可以更好地理解队列的基本操作和实现原理。 一、需求分析 ...
数据结构 -- C语言版 -- 队列的部分实现代码,详细介绍参考数据结构--队列的系列博文。链接为:https://blog.csdn.net/songshuai0223/category_9742561.html。
Java语言编写的数据结构-队列实现,包括顺序队列和链队列。
基于C语言的数据结构-队列queue
java基础笔记数据结构-队列,详细描述了队列的原理及其实现方式,基础数据结构。
算法与数据结构-队列第3章2.ppt
Java基础复习笔记06数据结构-队列~~~~~~~
数据结构--队列实验报告
数据结构-实验4-循环队列的实现和运算.docx数据结构-实验4-循环队列的实现和运算.docx数据结构-实验4-循环队列的实现和运算.docx数据结构-实验4-循环队列的实现和运算.docx数据结构-实验4-循环队列的实现和运算.docx...
该文档详细介绍了数据结构的队列知识。数据结构是程序设计中必不可少的部分。
VS2008编译通过 C++数据结构 -- 队列知识 算法精简高效
数据结构——队列实现银行排队
数据结构 c语言编写 队列的顺序存储 仅供初学者研究使用 仅供参考
能够根据实际情况选择合适的存储结构,解决实际问题。 2.实验内容: 利用循环队列模拟舞伴配对问题: 1、利用循环队列模拟舞伴配对问题。在舞会上,男、女各自排成一队。舞会开始时。依次从男队和女队的队头各出一...