java链式结构_java语言实现队列顺序结构与链式结构
本文主要向大家介紹了java語言實現隊列順序結構與鏈式結構,通過具體的內容向大家展示,希望對大家學習java語言有所幫助。
隊列的順序存儲結構實現
public class Queue{
private Object[] data=null;
private int maxSize; //隊列容量
private int front;? //隊列頭,允許刪除
private int rear;?? //隊列尾,允許插入
//構造函數
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize >=0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException("隊列已滿,無法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}
//返回隊首元素,但不刪除
public E peek(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
return (E) data[front];
}
}
//出隊
public E poll(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
E value = (E) data[front];? //保留隊列的front端的元素的值
data[front++] = null;???? //釋放隊列的front端的元素
return value;
}
}
//隊列長度
public int length(){
return rear-front;
}
}
循環隊列的順序存儲結構實現
import java.util.Arrays;
public class LoopQueue{
public Object[] data = null;
private int maxSize; // 隊列容量
private int rear;// 隊列尾,允許插入
private int front;// 隊列頭,允許刪除
private int size=0; //隊列當前長度
public LoopQueue() {
this(10);
}
public LoopQueue(int initialSize) {
if (initialSize >= 0) {
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear = 0;
} else {
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
// 判空
public boolean empty() {
return size == 0;
}
// 插入
public boolean add(E e) {
if (size == maxSize) {
throw new RuntimeException("隊列已滿,無法插入新的元素!");
} else {
data[rear] = e;
rear = (rear + 1)%maxSize;
size ++;
return true;
}
}
// 返回隊首元素,但不刪除
public E peek() {
if (empty()) {
throw new RuntimeException("空隊列異常!");
} else {
return (E) data[front];
}
}
// 出隊
public E poll() {
if (empty()) {
throw new RuntimeException("空隊列異常!");
} else {
E value = (E) data[front]; // 保留隊列的front端的元素的值
data[front] = null; // 釋放隊列的front端的元素
front = (front+1)%maxSize;? //隊首指針加1
size--;
return value;
}
}
// 隊列長度
public int length() {
return size;
}
//清空循環隊列
public void clear(){
Arrays.fill(data, null);
size = 0;
front = 0;
rear = 0;
}
}
隊列的鏈式存儲結構實現
public class LinkQueue{
// 鏈棧的節點
private class Node{
E e;
Nodenext;
public Node() {
}
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
}
private Node front;// 隊列頭,允許刪除
private Node rear;// 隊列尾,允許插入
private int size; //隊列當前長度
public LinkQueue() {
front = null;
rear = null;
}
//判空
public boolean empty(){
return size==0;
}
//插入
public boolean add(E e){
if(empty()){??? //如果隊列為空
front = new Node(e,null);//只有一個節點,front、rear都指向該節點
rear = front;
}else{
NodenewNode = new Node(e, null);
rear.next = newNode; //讓尾節點的next指向新增的節點
rear = newNode; //以新節點作為新的尾節點
}
size ++;
return true;
}
//返回隊首元素,但不刪除
public Nodepeek(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
return front;
}
}
//出隊
public Nodepoll(){
if(empty()){
throw new RuntimeException("空隊列異常!");
}else{
Nodevalue = front; //得到隊列頭元素
front = front.next;//讓front引用指向原隊列頭元素的下一個元素
value.next = null; //釋放原隊列頭元素的next引用
size --;
return value;
}
}
//隊列長度
public int length(){
return size;
}
}
本文由職坐標整理并發布,希望對同學們有所幫助。了解更多詳情請關注職坐標編程語言JAVA頻道!
總結
以上是生活随笔為你收集整理的java链式结构_java语言实现队列顺序结构与链式结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息学奥赛一本通 1173:阶乘和 |
- 下一篇: 聊天系统服务器端类图怎么画,聊天系统服务