日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java链式结构_java语言实现队列顺序结构与链式结构

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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语言实现队列顺序结构与链式结构的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。