码迷,mamicode.com
首页 > 其他好文 > 详细

使用LinkedList存储元素,基于LinkedList支持的方法实现自定义的队列(Queue)

时间:2018-02-21 16:32:22      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:自定义   ddl   turn   引号   int   ast   pop   ==   div   

  1 package com.jdk7.chapter4;
  2 
  3 import java.util.LinkedList;
  4 /**
  5  * LinkedList提供了addFirst/addLast、removeFirst/removeLast、getFirst/getLast、indexOf/lastIndexOf
  6  * 自定义的Queue则根据需要选取对应的函数,达到自定义的目的
  7  * 队列Queue从队尾进入从队首出来
  8  * @author Administrator
  9  *
 10  */
 11 public class Queue {
 12     //用来存放队列元素
 13     private LinkedList linkedList = new LinkedList();
 14     
 15     /**
 16      * 往队尾插入数据
 17      * @param obj
 18      */
 19     public void add(Object obj){
 20         linkedList.addLast(obj);
 21     }
 22     
 23     /**
 24      * 查询队首元素
 25      */
 26     public Object peek(){
 27         if(linkedList.isEmpty()){
 28             return null;
 29         }
 30         return linkedList.getFirst();
 31     }
 32     
 33     /**
 34      * 弹出队首元素
 35      * @return
 36      */
 37     public Object pop(){
 38         if(linkedList.isEmpty()){
 39             return null;
 40         }
 41         return linkedList.removeFirst();
 42     }
 43     
 44     /**
 45      * 删除队首元素是否成功
 46      * @return
 47      */
 48     public boolean remove(){
 49         if(linkedList.isEmpty()){
 50             return false;
 51         }
 52         linkedList.removeFirst();
 53         return true;
 54     }
 55     
 56     /**
 57      * 第一次出现obj的索引号
 58      * @param obj
 59      * @return
 60      */
 61     public int indexOf(Object obj){
 62         return linkedList.indexOf(obj);
 63     }
 64     
 65     /**
 66      * 最后一次出现obj的索引号
 67      * @param obj
 68      * @return
 69      */
 70     public int lastIndexOf(Object obj){
 71         return linkedList.lastIndexOf(obj);
 72     }
 73     
 74     public void clear(){
 75         linkedList.clear();
 76     }
 77     
 78     public void printLinkedList(LinkedList link){
 79         if(link==null){
 80             System.out.println("队列为空!");
 81         }
 82         System.out.print("队列所有元素为:");
 83         for(int i=0;i<link.size();i++){
 84             System.out.print(link.get(i)+" ");
 85         }
 86         System.out.println();
 87     }
 88     public static void main(String[] args) {
 89         Queue queue = new Queue();
 90         queue.add("qqqq");
 91         queue.add("wwww");
 92         queue.add("eeee");
 93         queue.add("rrrr");
 94         queue.add("tttt");
 95         queue.add("rrrr");
 96         queue.printLinkedList(queue.linkedList);
 97         System.out.println("队首元素为: "+queue.peek());
 98         System.out.println("弹出队首元素: "+queue.pop());
 99         queue.printLinkedList(queue.linkedList);
100         System.out.println("移除对首元素: "+queue.remove());
101         queue.printLinkedList(queue.linkedList);
102         System.out.println("第一次出现‘qqqq‘的索引号: "+queue.indexOf("rrrr"));
103         System.out.println("最后一次出现‘qqqq‘的索引号: "+queue.lastIndexOf("rrrr"));
104         queue.clear();
105         queue.printLinkedList(queue.linkedList);
106     }
107 }
108 
109 执行结果:
110 队列所有元素为:qqqq wwww eeee rrrr tttt rrrr 
111 队首元素为: qqqq
112 弹出队首元素: qqqq
113 队列所有元素为:wwww eeee rrrr tttt rrrr 
114 移除对首元素: true
115 队列所有元素为:eeee rrrr tttt rrrr 
116 第一次出现‘qqqq‘的索引号: 1
117 最后一次出现‘qqqq‘的索引号: 3
118 队列所有元素为:

 

使用LinkedList存储元素,基于LinkedList支持的方法实现自定义的队列(Queue)

标签:自定义   ddl   turn   引号   int   ast   pop   ==   div   

原文地址:https://www.cnblogs.com/celine/p/8456714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!