Algorithm 构建一个限制为数组大小5的队列-但队列可能会增长

Algorithm 构建一个限制为数组大小5的队列-但队列可能会增长,algorithm,data-structures,Algorithm,Data Structures,问题:使用enqueue和dequeue方法构建队列类。但是,您使用的语言有一个bug,它不允许数组存储超过5个元素,您将如何构建它 我写了下面的代码,我被拒绝了,好奇地想知道是什么问题,为什么我被拒绝,仅供参考-这是在技术电话屏幕 static LinkedList<Queue<Integer>> list = new LinkedList<Queue<Integer>>(); public static void enQueue(Intege

问题:使用enqueue和dequeue方法构建队列类。但是,您使用的语言有一个bug,它不允许数组存储超过5个元素,您将如何构建它

我写了下面的代码,我被拒绝了,好奇地想知道是什么问题,为什么我被拒绝,仅供参考-这是在技术电话屏幕

static LinkedList<Queue<Integer>> list = new LinkedList<Queue<Integer>>();

public static void enQueue(Integer element){

    //iterating the linkedlist....
    while(true){ //todo
        Queue<Integer> curr;
        if(list.size() > 0)
            curr = list.getLast();
        else{
            curr = new LinkedList<Integer>();
            list.add(curr);
        }
        if(curr.size() < 5){
            curr.add(element);
            System.out.println("enqueued:" + element);
            break;
        } else{
            Queue<Integer> newQueue = new LinkedList<Integer>();
            newQueue.add(element);
            list.add(newQueue); //adding the new Queue of size(5) to the list
            break;
        }
    }
}

public static Integer deQueue(){
    LinkedList<Queue<Integer>> node = list;
    if(!isEmpty()){
        Integer retValue = 0;
        while(list.size() > 0){
            Queue<Integer> q = node.getFirst();
            if(!q.isEmpty()){
                retValue =  q.poll();
                break;
            }else{
                node.removeFirst();
            }
        }
        return retValue;
    }else{
        System.out.println("No such element found");
        return -1; //throw new NoSuchElementException("no element found");
    }
}

public static boolean isEmpty(){
    return list.size() == 0;
}
staticlinkedlist=newlinkedlist();
公共静态void排队(整数元素){
//正在迭代linkedlist。。。。
while(true){//todo
排队货币;
如果(list.size()>0)
curr=list.getLast();
否则{
curr=新链接列表();
列表。添加(当前);
}
如果(当前大小()<5){
当前添加(元素);
System.out.println(“排队:+元素);
打破
}否则{
Queue newQueue=newlinkedlist();
添加(元素);
list.add(newQueue);//将大小为(5)的新队列添加到列表中
打破
}
}
}
公共静态整数出列(){
LinkedList节点=列表;
如果(!isEmpty()){
整数值=0;
while(list.size()>0){
队列q=node.getFirst();
如果(!q.isEmpty()){
retValue=q.poll();
打破
}否则{
node.removeFirst();
}
}
返回值;
}否则{
System.out.println(“未找到此类元素”);
return-1;//抛出新的NoSuchElementException(“未找到元素”);
}
}
公共静态布尔值isEmpty(){
返回列表。size()==0;
}

我想问题是检查队列数据结构与数组的实现,数组的大小不能超过5

我想要点像这样的

Class Queue {

int[][] data;
int maxSize = 25; //5 * 5
int[] front;
int[] rear;

Enqeue(int element) {
   //Check for queue full
   //Increment front last index, if it crosses 5. Increment previous index
   //Use the front index identify data element
   //Put the data
}

Dequeue() {
   //Check for queue empty

   //Use the rear index identify data element
   //get the data
   //Increment rear last index, if it crosses 5. Increment previous index
   //Return the data

}



}
队列大小可以很容易地扩展到大的数字


希望有帮助

我想问题是检查队列数据结构与数组的实现,数组的大小不能超过5

我想要点像这样的

Class Queue {

int[][] data;
int maxSize = 25; //5 * 5
int[] front;
int[] rear;

Enqeue(int element) {
   //Check for queue full
   //Increment front last index, if it crosses 5. Increment previous index
   //Use the front index identify data element
   //Put the data
}

Dequeue() {
   //Check for queue empty

   //Use the rear index identify data element
   //get the data
   //Increment rear last index, if it crosses 5. Increment previous index
   //Return the data

}



}
队列大小可以很容易地扩展到大的数字


希望有帮助

您被拒绝,因为:

  • 您假设了一个预先存在的队列类,这就是您应该实现的
  • 您使用了LinkedList,它已经实现了类似队列的接口,而不是实际实现队列
  • 你对LinkedList的使用很糟糕——根本没有理由进行任何循环。您可以让您的入队和出队调用LinkedList.add()LinkedList.pollFirst()

面试官真的想让你实际执行一个队列。数组限制是强制您使用链接实现,面试官希望看到您正确维护链接。

您被拒绝,因为:

  • 您假设了一个预先存在的队列类,这就是您应该实现的
  • 您使用了LinkedList,它已经实现了类似队列的接口,而不是实际实现队列
  • 你对LinkedList的使用很糟糕——根本没有理由进行任何循环。您可以让您的入队和出队调用LinkedList.add()LinkedList.pollFirst()

面试官真的想让你实际执行一个队列。数组限制是为了迫使您使用链接实现,而面试官希望看到您正确维护链接。

面试官不太可能接受大小限制。任何队列或数据结构都有一个上限。这就是为什么我们有“队列已满”异常。我想面试官是想看看你是如何克服限制的。您可以轻松地将上述内容更改为125625,。。只需在数据中添加另一个维度,前后指针。不,没有理由有这样的上限。例如,LinkedList没有任何这样的上限,并且不包含超过5个元素的数组。同意链表实现没有上限。到目前为止,您可能还希望实现“队列已满”情况。可能有数百万。看起来基于链表的实现是这种情况的干净解决方案。感谢Arun的建议,但我们无法获得队列的最大大小。。是的,链表是链接5元素数组的选项之一。面试官不太可能接受大小限制。任何队列或数据结构都有一个上限。这就是为什么我们有“队列已满”异常。我想面试官是想看看你是如何克服限制的。您可以轻松地将上述内容更改为125625,。。只需在数据中添加另一个维度,前后指针。不,没有理由有这样的上限。例如,LinkedList没有任何这样的上限,并且不包含超过5个元素的数组。同意链表实现没有上限。到目前为止,您可能还希望实现“队列已满”情况。可能有数百万。看起来基于链表的实现是这种情况的干净解决方案。感谢Arun的建议,但我们无法获得队列的最大大小。。是链表是链接5元素数组的选项之一。如何将队列本身实现为链表?也就是说,完全忘掉数组。把队列本身实现为链表怎么样?也就是说,完全忘记阵列。谢谢Matt的回复,我同意我不需要循环。。但使用java集合是可以的,问题不是写我自己的队列,唯一的限制是数组的大小不能超过5。。我想我使用了while(true)和break,这是非常糟糕的,我在匆忙中使用了它,当时我正试图在匆忙中执行我的代码..谢谢Matt的回复,我同意我不需要循环。。但是使用java集合是可以的,问题不是写我自己的队列,唯一的限制是数组不能使用h