【问题标题】:enqueue a linked list - java将链表排入队列 - java
【发布时间】:2014-11-20 03:46:00
【问题描述】:

对于一个作业(即将到期),我必须将一个我手动实现的链表排入队列(不使用 java 的内置类)。链表如下:

public class SnocList {
    private char c;
    private SnocList l;

    public SnocList(){
    }
    public SnocList (char x, SnocList y) {
        this.c = x;
        this.l = y;
    }
    public char getC(){
        return this.c;
    }
    public SnocList getL(){
        return this.l;
    }
    public void setNext(SnocList input){
        this.l = input;
    }

    public boolean isEmpty() {
        if (this.c == 0) return true;
        return false;
    }
}

我的队列类如下:

public class SnocQueue {
    private SnocList list;

    public SnocQueue(){
        this.list = new SnocList();
    }

    public void enqueue(char c){
        //I don't know what to put here
    }
}

我不知道该怎么做。这显然很简单,但我不知道该怎么做。对于那些想要提供帮助的人,enqueue 会将一个新节点添加到上一个列表的空引用(空指针)所在的列表中。

['a' | --]-->[ 'b' | --]-->[ 'c' | null ] 在此处添加新节点

【问题讨论】:

    标签: java linked-list queue


    【解决方案1】:

    要入队,您还需要一个“尾”字段和一个“头”字段,在此示例中,我将创建一个“项目”类的对象队列,您可以使用您需要的节点

    public class Item {
        private int value;
        private Item next;
    
        public Item(int val) {
            this.value = val;
        }
    
        public void setNext(Item item) {
            this.next = item;
        }
    }
    

    还有队列:

    public class SomeQueue {
        private Item head;
        private Item tail;
        private int size;//size is used to verify if it has Items and avoid null references
    
        public SomeQueue(){
            this.size = 0;
        }
        public void enqueue(Item item) {
            if (this.size > 0) {
                this.tail.setNext(item);
                this.tail = item;
            } else { //this is when the size is 0, it means is empty
                this.head = item;
                this.tail = item;
             }
            this.size++;
        }
    }
    

    正如您看到队列的重要内容是头部和尾部,方法 setNext 也很重要,用于在节点之间进行引用。

    【讨论】:

      【解决方案2】:
      1. 创建一个 SnocList 头节点 - 这是第一个 链表中的 SnocList 节点。
      2. 创建入队方法
      3. 在enqueue方法中,首先检查head SnocList是否为 空(或 == null)。如果是这样,只需制作头部 SnocList 包含您传入的字符和对l SnocList 的引用的新SnocList。 l 将是 null 因为不会有其他 链表中的SnocLists。然后,return 结束方法的执行。
      4. 如果 head 存在 (else),则使用 while 循环遍历链表,直到找到对 l 的引用为 null 的 SnocList。
      5. 通过将新的 SnocList 对象 new SnocList(ch, curr.l) 分配给先前为空的 curr.l,将新的 SnocList 附加到链表的末尾。

        public class SnocQueue {
         SnocList head;
        
         public void enqueue(char ch) {
              if(head == null) {
                 head = new SnocList(ch, null);
                 return;
              } else {
                 SnocList curr = head;
                 while (curr.l != null) {
                     curr = curr.l;
                 }
                 curr.l = new SnocList(ch, curr.l);
              }
         }
        } 
        

      【讨论】:

        猜你喜欢
        • 2018-03-16
        • 2018-05-10
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        相关资源
        最近更新 更多