Implementing queue in java

后端 未结 3 2024
攒了一身酷
攒了一身酷 2021-01-29 14:16

Implementing a queue in Java is pretty common interview question. I surfed online and saw many implementations where they do fancy stuff like implementing queue interface and wr

3条回答
  •  清酒与你
    2021-01-29 14:49

    I have very recently gone through these kind of interview questions.

    Using set methods to add,remove, chekForEmpty etc from a list is a general way to implement a queue.

    for example :

       public void enqueue(E item) {
         list.addLast(item);
         }
    
       public E dequeue() {
          return list.poll();
          }
    
       public boolean hasItems() {
          return !list.isEmpty();
          }
    
       public int size() {
          return list.size();
          }
    
       public void addItems(GenQueue l) {
          while (l.hasItems())
            list.addLast(l.dequeue());
            }
    

提交回复
热议问题