队列——java实现
来源:互联网 发布:网络漫画家收入 编辑:程序博客网 时间:2024/05/16 11:34
定义
现实生活中,我们去排队买东西,先排的先走,后排的后走。在程序中,这就是队列即先进先出(First in first out)表.
结构
实现
本文使用链表实现队列,如下:
package com.lemon.queue;import java.util.NoSuchElementException;/** * this class using linked list realize queue * the feature of queue is first in first out,more specifically, * add operation only at the tail of queue and remove only at the top of queue. * @author an.yan * @param <T> */public class MyQueue<T> {public static class Node<T>{private T value;private Node<T> next;public Node(T value, Node<T> next) {this.value = value;this.next = next;}}private int size;private Node<T> topOfQueue;public MyQueue(){topOfQueue=new Node<T>(null, null);}public boolean isEmpty(){return size==0;}/** * add element to the tail of queue * @param element * @return */ public boolean add(T element){ Node<T> node=new Node<T>(element, null); if(size==0){ topOfQueue=node; }else{ Node<T> temp=topOfQueue; while(true){ if(temp.next==null){ break; } temp=temp.next; } temp.next=node; } size++; return true; } /** * get and remove the value which at the top of current queue * @return */public T remove(){T t=topOfQueue.value;topOfQueue=topOfQueue.next;size--;return t;}/** * return,but don't remove, the value which at the top of current queue * @return */public T element(){if(isEmpty()){throw new NoSuchElementException("current queue is null!");}return topOfQueue.value;}/** * @param args */public static void main(String[] args) {MyQueue<String> queue=new MyQueue<String>();//queue.add("a");//queue.add("b");//queue.add("c");//queue.add("d");//queue.add("e");//queue.add("f");/** * test remove *///while(!queue.isEmpty()){//System.out.println(queue.remove());//}/** * test element() */System.out.println(queue.element());}}
应用
writing soon
- 队列——java实现
- 队列——Java实现
- 数据结构Java实现——②队列
- 数据结构(Java)——队列的实现
- JAVA基础训练——用链表实现队列
- 数据结构——Java实现队列
- 数据结构Java实现——②队列--->队列的“奇葩”一循环顺序队列
- 数据结构Java实现——②队列-->队列的“奇葩”二 优先级队列
- 数据结构——使用双端链表实现队列(java实现)
- 队列:循环队列Java实现
- 数据结构的Java实现——栈和队列
- java实现数据结构——栈Stack与队列Queue
- java专题——双栈实现队列
- java专题——双队列实现栈
- 队列的应用——火车重排(java实现)
- java实现队列
- java 实现的队列
- java 队列实现
- 3D引擎研究初始
- GAMIT视频资料整理
- C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
- JavaFX示例--简易图片处理工具
- 《miniOS分析》前言
- 队列——java实现
- 苹果三星再包揽手机利润 掀新一轮火拼
- arcgis server(Flex) 获取图层的字段值
- Android应用性能优化之使用SparseArray替代HashMap
- 堆上的溢出
- linux man使用
- 操作系统引导过程探究
- 亚马逊应用商店:破局中国市场 挑衅谷歌
- assert()函数用法总结