zoukankan      html  css  js  c++  java
  • 《模拟队列或堆栈》

     1 package cn.itcast.api.b.list.subclass;
     2 
     3 import java.util.LinkedList;
     4 
     5 public class LinkedListTest {
     6 
     7     public static void main(String[] args) {
     8         /*
     9          * 面试题:用LinkedList模拟一个堆栈或者队列数据结构.
    10          * 创建一个堆栈或者队列数据结构对象.该对象中是使用LinkedList来完成的。
    11          * 
    12          */
    13         //创建一个队列对象.
    14         Queue queue = new Queue();
    15         //往队列中添加元素.
    16         queue.myAdd("itcast1");
    17         queue.myAdd("itcast2");
    18         queue.myAdd("itcast3");
    19         queue.myAdd("itcast4");
    20         
    21         while(!queue.isNull()){
    22             System.out.println(queue.myGet());
    23         }
    24 
    25     }
    26 
    27 }
    28 
    29 /**
    30  * 定义一个队列数据结构。Queue
    31  * 
    32  */
    33 class Queue{
    34     //封装了一个链表数据结构.
    35     private LinkedList link;
    36     /*
    37      * 队列初始化时,对链表对象初始化.
    38      */
    39     Queue(){
    40         link = new LinkedList();
    41     }
    42     /*
    43      *队列的添加功能. 
    44      */
    45     public void myAdd(Object obj){
    46         //内部使用的就是链表的方法.
    47         link.addFirst(obj);
    48     }
    49     
    50     /**
    51      * 队列的获取方法.
    52      */
    53     public Object myGet(){
    54         return link.removeLast();
    55     }
    56     //判断队列中元素是否空,没有元素就为true.
    57     public boolean isNull(){
    58         return link.isEmpty();
    59     }
    60     
    61 }
  • 相关阅读:
    Add source code and doc in maven
    Spring toturial note
    How to add local jar into maven project
    Ubuntu 12.04 下安装 Eclipse
    如何更改linux文件的拥有者及用户组(chown和chgrp)
    20非常有用的Java程序片段
    Java中的Set、List、Map的区别
    Java I/O知识点汇总
    Java I/O流整理
    hadoop2.0集群配置
  • 原文地址:https://www.cnblogs.com/sun-/p/5468713.html
Copyright © 2011-2022 走看看