设为首页 加入收藏

TOP

Java实现栈和队列(二)
2015-07-20 12:52:50 来源: 作者: 【 】 浏览:95
Tags:Java 实现 队列
+]=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;
? ? ? ? Node next;


? ? ? ? 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{
? ? ? ? ? ? ? Node newNode = new Node(e, null);
? ? ? ? ? ? ? rear.next = newNode; //让尾节点的next指向新增的节点
? ? ? ? ? ? ? rear = newNode; //以新节点作为新的尾节点
? ? ? ? ? }
? ? ? ? ? size ++;
? ? ? ? ? return true;
? ? ? }
? ? ?
? ? ? //返回队首元素,但不删除
? ? ? public Node peek(){
? ? ? ? ? if(empty()){
? ? ? ? ? ? ? throw new RuntimeException("空队列异常!");
? ? ? ? ? }else{
? ? ? ? ? ? ? return front;
? ? ? ? ? }
? ? ? }
? ? ?
? ? ? //出队
? ? ? public Node poll(){
? ? ? ? ? if(empty()){
? ? ? ? ? ? ? throw new RuntimeException("空队列异常!");
? ? ? ? ? }else{
? ? ? ? ? ? ? Node value = front; //得到队列头元素
? ? ? ? ? ? ? front = front.next;//让front引用指向原队列头元素的下一个元素
? ? ? ? ? ? ? value.next = null; //释放原队列头元素的next引用
? ? ? ? ? ? ? size --;
? ? ? ? ? ? ? return value;
? ? ? ? ? }? ? ? ?
? ? ? }
? ? ?
? ? ? //队列长度
? ? ? public int length(){
? ? ? ? ? return size;
? ? ? }
}


基于LinkedList实现队列结构


/**
?* 使用java.util.Queue接口,其底层关联到一个LinkedList(双端队列)实例.
?*/
import java.util.LinkedList;
import java.util.Queue;


public class QueueList {
? ? private Queue queue = new LinkedList();
? ?
? ? // 将指定的元素插入此队列(如果立即可行且不会违反容量限制),在成功时返回 t

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java学习遇到的问题 下一篇Java实现线性表-顺序表示和链式表..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: