【问题标题】:Queue array implementation resize队列数组实现resize
【发布时间】:2013-11-22 05:14:13
【问题描述】:

我需要创建一个简单的队列数组实现,其中包含入队、出队、isEmpty 等基本方法。我唯一的问题是,当涉及到 resize 方法时,我卡住了,因为如果我想向我的队列添加更多值(具有固定大小,因为是一个数组)我不知道如何使它工作并将所有值保留在地方。 万一您想知道,一切正常,唯一的问题是我的调整大小不起作用(这里写的方法不是我尝试过的唯一方法)。 如果你想尝试一下,我也会把我的主要方法,希望你能帮助,谢谢。

主要方法:

  public class MainQueue {
              public static void main(String[] args) {

            int capacity=10;
            Queue<Integer> queue = new Queue<Integer>(capacity);

            queue.enqueue(1);
            queue.enqueue(2);
            queue.enqueue(3);
            queue.enqueue(4);
            queue.enqueue(5);
            queue.enqueue(6);
            queue.enqueue(7);
            queue.enqueue(8);
            queue.enqueue(9);
            queue.enqueue(10);

            System.out.println("Queue: "+ queue);

                            //WORKS SO FAR
                            queue.enqueue(11);
                            //11 is placed at the beginning of the queue  
                            //instead at the end and my last value is null (?)                              

类队列:

 import java.util.NoSuchElementException;


 public class Queue <E>{
 private E[] elements;//array in generic
 private int front;//first element or front of the queue
 private int back;//last element or back of the queue
 private int capacity; //capacity of the queue
 private int count; //indicates number of elements currently stored in the queue

        @SuppressWarnings("unchecked")
        public Queue(int size)
        {
            capacity = size;
            count = 0;
            back = size-1;
            front = 0;
            elements =(E []) new Object[size];  //array empty   
        }

    //Returns true if the queue is empty or false
    public boolean isEmpty()
        {
            return count==0;//means its true
        }

    //Add elements to the queue 
    public void enqueue(E item)
        {  
            if(count == capacity) 
            {  
                 resize(capacity*2);
                  // System.out.println("Queue is full");  

             }

            back =(back+1) % capacity;    //example back=(0+1)%10=1
            elements[back]=item;
            //elements[0]=0
            //item=elements[count];
            count++;
            }


    //Public resize
    public void resize(int reSize){
        E[] tmp = (E[]) new Object[reSize];

           int current = front;
           for (int i = 0; i < count; i++)
              {
              tmp[i] = elements[current];
              current = (current + 1) % count;
              }
         elements = tmp;

           }


    //Dequeue method to remove head
    public E dequeue() 
            {
                if(isEmpty())
                    throw new NoSuchElementException("Dequeue: Queue is empty");
                else
                {
                    count--;
                    for(int x = 1; x <= count; x++)
                    {
                        elements[x-1] = elements[x];
                    }
                    capacity--;
                    return (E) elements;
                }
            }

//peek the first element
    public E peek()   
        {
            if(isEmpty())  
               {  
                   throw new NoSuchElementException("Peek: Queue is empty");
               }

               else  
                   return elements[front];
         }


//Print queue as string
     public String toString()    
           {  

           if(isEmpty()) {
               System.out.println("Queue is empty.");
            //throw new NoSuchElementException("Queue is empty");
           }

               String s = "[";  
               for(int i = 0; i <count; i++)  
               {  
                   if(i != 0)  
                       s += ", ";  
                   s = s + elements[i];// [value1,value2,....]  
               }  

               s +="]";  
              return s;  
           }  

       public void delete() {   //Delete everything
           count = 0;
       }
       }

【问题讨论】:

  • Ups,对不起,我写了 queue.enque(11);而不是 queue.enqueue(11);
  • 你的方法reSize应该做什么?

标签: java arrays methods queue


【解决方案1】:

调整大小时忘记更新内容: 正面、容量和背面。

public void resize(int reSize){
    E[] tmp = (E[]) new Object[reSize];

       int current = front;
       for (int i = 0; i < count; i++)
          {
          tmp[i] = elements[current];
          current = (current + 1) % count;
          }
     elements = tmp;
     front = 0;
     back = count-1;
     capacity=reSize;
       }

【讨论】:

  • 记得添加 amalds 错误修复并在 mod 操作中使用容量:)
【解决方案2】:

在将展开队列的项目入队时调整大小几乎没有错误。

  1. 在调整大小算法中 当前 = (当前 + 1) % 计数;应该是(当前 + 1)% 容量

  2. 您必须在调整大小功能中更改容量值 容量=调整大小;

为什么在出队时要改变容量?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2012-01-30
    相关资源
    最近更新 更多