A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 myzhang 于 2014-9-5 23:14 编辑
  1. <div class="blockcode"><blockquote>package Day14;

  2. import java.util.LinkedList;
  3. public class LinkedListDemo {

  4.         /**面试题:用LinkedList模拟一个堆栈或者队列数据结构
  5.          * 分析:创建一个堆栈或者队列的数据结构对象,该对象结构中使用LinkedList来完成的。
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                 // TODO Auto-generated method stub
  10.                 //创建一个队列的对象
  11.                 Queue queue=new Queue();
  12.                 queue.myAdd("itacst1");
  13.                 queue.myAdd("itacst2");
  14.                 queue.myAdd("itacst3");
  15.                 queue.myAdd("itacst4");
  16.                 while (queue.isNull()) {
  17.                         System.out.println(queue.myGet());        
  18.                 }
  19.         }
  20.         class Queue{
  21.                 //封装一个LinkedLink链表结构
  22.                 private LinkedList linkedList;
  23.                 //构造函数初始化链表结构
  24.                 public Queue(){
  25.                         linkedList=new LinkedList();
  26.                 }
  27.                 //队列的添加元素功能
  28.                 public void myAdd(Object object){
  29.                         linkedList.addFirst(object);
  30.                 }
  31.                 //队列的获取方法
  32.                 public Object myGet(){
  33.                         return linkedList.removeLast();
  34.                 }
  35.                 //判断队列中元素是否为空,没有元素就为true。
  36.                 public boolean isNull(){
  37.                         return linkedList.isEmpty();
  38.                 }
  39.         }
  40. }
复制代码
请问大家上面的代码是体现队列结构吧,可是运行的时候发生了错误?提示如下:
No enclosing instance of type LinkedListDemo is accessible. Must qualify the allocation with an enclosing instance of type LinkedListDemo (e.g. x.new A() where x is an instance of LinkedListDemo).

2 个回复

倒序浏览
请问大家这是什么原因啊?
回复 使用道具 举报
这个是我的代码,楼主你的26行构造函数的处理上,有点问题
  1. package wjd.demo01;

  2. import java.util.LinkedList;

  3. public class LinkedDemo {
  4.         public static void main(String[] args) {
  5.                 Queue q = new Queue();
  6.                 q.my_add("a");
  7.                 q.my_add("b");
  8.                 q.my_add("c");
  9.                 while(!(q.isNull())){
  10.                         System.out.println(q.myGet_1());
  11.                 }
  12.         }
  13.        
  14. }

  15. class Queue{
  16.         LinkedList<String> ll = new LinkedList<String>();
  17.         public void my_add(String s){
  18.                 ll.addFirst(s);
  19.         }
  20.         public String myGet_1(){
  21.                 return ll.removeFirst();
  22.         }
  23.         public String myGet_2(){
  24.                 return ll.removeLast();
  25.         }
  26.         public boolean isNull(){
  27.                 return ll.isEmpty();
  28.         }
  29. }
复制代码
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马