【问题标题】:Queue using array in java在java中使用数组排队
【发布时间】:2014-02-21 13:38:56
【问题描述】:

我只想在队列中询问,例如我的队列中是否已经有 5 个元素并且它的限制是 5。然后,我删除了一个元素。然后我插入一个元素。输出会溢出对吗? 我的问题是即使从队列中插入了新元素。它总是说溢出。我想要的是在我的队列中添加更多元素。(我指的是上面的情况)

import java.io.*;  
import java.lang.*;  
class clrqueue  
{  
     DataInputStream get=new DataInputStream(System.in);  
     int a[];  
     int i,front=0,rear=0,n,item,count=0;  
void getdata()  
{  
    try  
    {  
         // to enter the limit

       System.out.println("Enter the limit");  
       n=Integer.parseInt(get.readLine());  
       a=new int[n];  
    }   
   catch(Exception e)  
    {  
       System.out.println(e.getMessage());  
   }  
}  
void enqueue()  
{  
  try  
   {  
     if(count<n)  
      {  
         System.out.println("Enter the element to be added:");  
         item=Integer.parseInt(get.readLine());  
         a[rear]=item;  
         rear++;  
         count++;  
      }  
     else  
         System.out.println("QUEUE IS FULL");  
   }  
   catch(Exception e)  
   {  
         System.out.println(e.getMessage());  
   }  
}  


    void dequeue()  
      {  
        if(count!=0)  
         {  
    System.out.println("The item deleted is:"+a[front]);  
    front++;  
    count--;  
    }  
   else  
    System.out.println("QUEUE IS EMPTY");  
  if(rear==n)  
   rear=0;  
  }  
  void display()  
  {  
   int m=0;  
   if(count==0)  
   System.out.println("QUEUE IS EMPTY");  
   else  
   {  
   for(i=front;m<count;i++,m++)  
   System.out.println(" "+a[i]);  
   }  
  }  
 }  
 class Myqueue  
 {  
  public static void main(String arg[])  
  {  
  DataInputStream get=new DataInputStream(System.in);  
  int ch;  
  clrqueue obj=new clrqueue();  
  obj.getdata();  
  try  
  {  
   do  
   {  
   System.out.println(" 1.Enqueue  2.Dequeue  3.Display  4.Exit");  
   System.out.println("Enter the choice");  
   ch=Integer.parseInt(get.readLine());  
   switch (ch)  
   {  
   case 1:  
       obj.enqueue();  
      break;  
   case 2:  
      obj.dequeue();  
      break;  
   case 3:  
      obj.display();  
      break;  
   }  
   }  
   while(ch!=4);  
  }  
  catch(Exception e)  
  {  
  System.out.println(e.getMessage());  
  }  
  }  
 }  

【问题讨论】:

  • 变量名没有名字只是因为它们必须,而是因为它们应该有意义。 a、n、m 或 ch不是。请使用一些能对读者说明问题的名称,因为要快速理解您的代码要困难得多。

标签: java queue


【解决方案1】:

您想实现一个循环队列。 关键是,请小心保管前后指针,否则会出现溢出或完整消息。请注意,当你做front++,rear++时,你可以让它们=n,每次做一个mod n都会帮助它在必要时恢复为0。

front++;front%=n;

rear++;rear%=n;

for(i=front;m<count;i++,i%=n,m++) 

你的实现几乎是正确的,继续。

【讨论】:

    【解决方案2】:

    由于您使用的是固定大小的数组,因此当您删除元素时,您必须移动数组中的所有剩余元素。就目前而言,您只是继续增加rear 和front,并导致溢出,因为这些变量最终超出了数组中的可用索引。您要么需要在执行操作时移动数组,要么相应地包装 rear 和 front 变量,使它们始终介于 0 和限制之间。

    在你的出队中试试这个。计数和后部递减,因为数组向左移动了 1。

    void dequeue() {
        if (count != 0) {
            System.out.println("The item deleted is:" + a[front]);
            count--;
            rear--;
            System.arraycopy(a, 1, a, 0, count);
        } else
            System.out.println("QUEUE IS EMPTY");
        if (rear == n)
            rear = 0;
    }
    

    arraycopy 方法将一个数组的内容复制到另一个数组中。第一个参数是源/当前数组,第二个参数是起始位置。您要删除数组中的第一个元素,因此起始位置为 1(数组中的第二个元素)。第三个参数是要复制数据的目标数组。在这种情况下,我只是复制到同一个数组。第四个参数是要添加数据的目标数组的起始索引。这是 0,因为我们想将该元素放在数组的前面。最后一个参数是要复制的元素数量。 a[1]复制到a[0],a[2]复制到a[1]等等。

    因此,例如,如果数组是 [1, 2, 3, 4],那么当我们复制时,计数将为 3(当前计数已递减,我们只复制最后的 3 个元素)。由于起始参数为 1,这意味着首先复制的是 a[1]。

    我们最终得到一个数组,如:[2,3,4]

    您可以在此处阅读更多信息:oracle docs

    this other stackoverflow question也说明了如何用for循环做同样的事情: Java, Shifting Elements in an Array

    【讨论】:

    • System.arraycopy(a, 1, a, 0, count) 有什么用?
    • @user3249541 为问题添加了额外的说明。
    • 我还有最后一个问题。当我更改代码时,我的打印方法不起作用。你能帮忙吗?
    • @user3249541 你能更具体地谈谈你的问题吗?很可能您在更新代码时不小心删除或更改了某些内容。我建议将您之前的代码与新代码进行比较,看看差异在哪里。
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2018-05-10
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多