显示和插入方法错误(双端队列)

发布于 2024-09-02 13:56:47 字数 4804 浏览 5 评论 0原文

1)

当我从右侧或左侧程序中删除时,我的问题将被删除 true 但是当我调用 diplay 方法时,内容错误

如下 我插入 12 43 65 23 当从左侧删除程序时,将删除 12 但是当调用显示方法时显示如下 12 43 65

并且当从右侧程序中删除时将删除 23 但是当调用显示方法时显示像这样 12 43

为什么?????? );

当我尝试在删除后插入时,写此

无法正确插入,因为队列已满。先删除右边,然后你可以插入右边,

问题出在哪里?

请帮助

2) 我的代码

第一类

class dqueue
{
    private int fullsize;                    //number of all cells
    private int item_num;                    // number of busy cells only
    private int front,rear;
    public int j;
    private double [] dqarr;
//==========================================
    public dqueue(int s)                      //constructor
    {
        fullsize = s;
        front = 0;
        rear = -1;
        item_num = 0;
        dqarr = new double[fullsize];
    }
//==========================================
    public void insert(double data)
    {
        if (rear == fullsize-1)
            rear = -1;
        rear++;
        dqarr[rear] = data;
        item_num++;

    }
   public double removeLeft() // take item from front of queue
     {
   double temp = dqarr[front++]; // get value and incr front
   if(front == fullsize) 
   front = 0;
   item_num --; // one less item
   return temp;
    }
    public double removeRight() // take item from rear of queue
     {
   double temp = dqarr[rear--]; // get value and decr rear
   if(rear == -1) // 
   rear = item_num -1;
   item_num --; // one less item
   return temp;
    }
//=========================================

     public void display ()                //display items
{
for (int j=0;j<item_num;j++)               // for every element 
System.out.print(dqarr[j] +"  " );          // display it 
System.out.println("");
}

//=========================================
public int size()              //number of items in queue
{
return item_num;
}
//==========================================
public boolean isEmpty()       // true if queue is empty
{
return (item_num ==0);
}


} 

第二类

import java.util.Scanner;
class dqueuetest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("              ***** Welcome here***** ");
        System.out.println("              *****  Mind Of Programming Group***** ");
       System.out.println("               _____________________________________________ ");
        System.out.println("enter size of your dqueue");
        int size = input.nextInt();
        dqueue mydq = new dqueue(size);
        System.out.println("");
        System.out.println("enter your itemes");
//=====================================
        for(int i = 0;i<=size-1;i++)
        {
           System.out.printf("item %d:",i+1);
           double item = input.nextDouble();
           mydq.insert(item);
           System.out.println("");
        }    

//=====================================  
int queue =size ;         
int c = 0 ;
while (c != 6)
{
    System.out.println("");
    System.out.println("************************************************");
    System.out.println("               MAIN MENUE");
    System.out.println("1- INSERT RIGHT  ");
    System.out.println("2- REMOVE LEFT");
    System.out.println("3- REMOVE RIGHT");
    System.out.println("4- DISPLAY");
    System.out.println("5- SIZE");
    System.out.println("6- EXIT");  
    System.out.println("************************************************");
    System.out.println("choose your operation by number(1-6)");  
    c = input.nextInt();
    switch  (c)
    {
    case 1:
   if (queue == size)
    System.out.print("Can not insert right because the queue is full . first remove right and then u can insert right   ");
          else { System.out.print("enter your item: ");
           double item = input.nextDouble();
           mydq.insert(item);}
          break;

   case 2:
       System.out.println("REMOVE FROM REAR :");
              if( !mydq.isEmpty() )
    {
  double item = mydq.removeLeft();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();

    break;

   case 3:
    System.out.println("REMOVE FROM FRONT :");
            if( !mydq.isEmpty() )
    {
  double item = mydq.removeRight();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();
     break;

    case 4:
    System.out.println("The items in Queue are :");  
        mydq.display();
    break;

    case 5:
     System.out.println("The  Size of the Queue is :"+mydq.size());
    break;

    case 6:
    System.out.println("Good Bye");

    break;

    default:
    System.out.println("wrong chiose enter again");
  }       //end switch
 }       //end while
}        // end main     

}//end class

1) My problem

when i make remove from right or left program will be remove true
but when i call diplay method the content wrong

like this
I insert 12 43 65 23
and when make remove from left program will remove 12
but when call display method show like this 12 43 65

and when make remove from right program will remove 23
but when call display method show like this 12 43

Why ?????? );

and when i try to make insert after remove write this

Can not insert right because the queue is full . first remove right and then u can insert right

where is the problem ??

Please Help me

please

2) My code

FIRST CLASS

class dqueue
{
    private int fullsize;                    //number of all cells
    private int item_num;                    // number of busy cells only
    private int front,rear;
    public int j;
    private double [] dqarr;
//==========================================
    public dqueue(int s)                      //constructor
    {
        fullsize = s;
        front = 0;
        rear = -1;
        item_num = 0;
        dqarr = new double[fullsize];
    }
//==========================================
    public void insert(double data)
    {
        if (rear == fullsize-1)
            rear = -1;
        rear++;
        dqarr[rear] = data;
        item_num++;

    }
   public double removeLeft() // take item from front of queue
     {
   double temp = dqarr[front++]; // get value and incr front
   if(front == fullsize) 
   front = 0;
   item_num --; // one less item
   return temp;
    }
    public double removeRight() // take item from rear of queue
     {
   double temp = dqarr[rear--]; // get value and decr rear
   if(rear == -1) // 
   rear = item_num -1;
   item_num --; // one less item
   return temp;
    }
//=========================================

     public void display ()                //display items
{
for (int j=0;j<item_num;j++)               // for every element 
System.out.print(dqarr[j] +"  " );          // display it 
System.out.println("");
}

//=========================================
public int size()              //number of items in queue
{
return item_num;
}
//==========================================
public boolean isEmpty()       // true if queue is empty
{
return (item_num ==0);
}


} 

SECOND CLASS

import java.util.Scanner;
class dqueuetest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("              ***** Welcome here***** ");
        System.out.println("              *****  Mind Of Programming Group***** ");
       System.out.println("               _____________________________________________ ");
        System.out.println("enter size of your dqueue");
        int size = input.nextInt();
        dqueue mydq = new dqueue(size);
        System.out.println("");
        System.out.println("enter your itemes");
//=====================================
        for(int i = 0;i<=size-1;i++)
        {
           System.out.printf("item %d:",i+1);
           double item = input.nextDouble();
           mydq.insert(item);
           System.out.println("");
        }    

//=====================================  
int queue =size ;         
int c = 0 ;
while (c != 6)
{
    System.out.println("");
    System.out.println("************************************************");
    System.out.println("               MAIN MENUE");
    System.out.println("1- INSERT RIGHT  ");
    System.out.println("2- REMOVE LEFT");
    System.out.println("3- REMOVE RIGHT");
    System.out.println("4- DISPLAY");
    System.out.println("5- SIZE");
    System.out.println("6- EXIT");  
    System.out.println("************************************************");
    System.out.println("choose your operation by number(1-6)");  
    c = input.nextInt();
    switch  (c)
    {
    case 1:
   if (queue == size)
    System.out.print("Can not insert right because the queue is full . first remove right and then u can insert right   ");
          else { System.out.print("enter your item: ");
           double item = input.nextDouble();
           mydq.insert(item);}
          break;

   case 2:
       System.out.println("REMOVE FROM REAR :");
              if( !mydq.isEmpty() )
    {
  double item = mydq.removeLeft();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();

    break;

   case 3:
    System.out.println("REMOVE FROM FRONT :");
            if( !mydq.isEmpty() )
    {
  double item = mydq.removeRight();

  System.out.print(item + "\t");
    } // end while
  System.out.println("");
  mydq.display();
     break;

    case 4:
    System.out.println("The items in Queue are :");  
        mydq.display();
    break;

    case 5:
     System.out.println("The  Size of the Queue is :"+mydq.size());
    break;

    case 6:
    System.out.println("Good Bye");

    break;

    default:
    System.out.println("wrong chiose enter again");
  }       //end switch
 }       //end while
}        // end main     

}//end class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生一梦 2024-09-09 13:56:47

我怀疑最让您困惑的是您的 display 方法...:

public void display ()                //display items
{
    for (int j=0;j<item_num;j++)               // for every element 
        System.out.print(dqarr[j] +"  " );          // display it 
    System.out.println("");
}

在这种方法中,您完全忽略了您编码的队列的“内部逻辑结构”,这主要取决于 frontrear 索引:事实上,请注意,您甚至没有在这里提及这两个索引 - 您只需打印“物理上第一个”item_num 元素,与“逻辑上存在”的元素没有任何关系。

相反,您需要从第 front 个元素(不是0 个元素!)开始并显示 item_num 元素从那里开始(当然,如果你遇到它,最后会环绕)。这将显示队列的逻辑内容,而不是实现它的数组的“随机”切片!-)

I suspect that's what confusing you most is your display method...:

public void display ()                //display items
{
    for (int j=0;j<item_num;j++)               // for every element 
        System.out.print(dqarr[j] +"  " );          // display it 
    System.out.println("");
}

in this method you're completely ignored the "internal logical structure" of the queue you have coded, which depends crucially on the front and rear indices: indeed, note you don't even mention either of those indices here -- you just print the "physically first" item_num elements, which have no relationship whatsoever with the "logically present" ones.

You need, instead, to start with the frontth element (not the 0th one!) and show the item_num elements starting there (with wraparound at the end if you meet it, of course). This will display the logical contents of your queue instead of a "random-oid" slice of the array that implements it!-)

且行且努力 2024-09-09 13:56:47

display 的实现从 0 到 item_num 迭代支持数组,因此它打印了错误的值。它应该从 frontrear 迭代(检查是否将索引环绕在缓冲区末尾)以打印出实际内容队列的:

public void display() {
  int j = front;
  for (int count = 0; count < item_num; count++) {
    System.out.print(dqarr[j] + "  ");
    j = increment(j);
  }
  System.out.println();
}

private int increment(int index) {
  if (index == fullsize - 1)
    return 0;
  return ++index;
}

我定义了一种 increment 方法来在一个位置实现索引步进 - 您可以在代码中的多个位置使用它。

还要注意,您的实现中没有溢出检查:如果我将 fullsize + 1 元素添加到队列中,第一个元素将被静默覆盖。

更新:我意识到在您的队列实现中rear对队列的最后一个元素进行索引(而不是像往常一样在最后一个元素之后索引在我见过的出队实现中),所以我相应地修改了代码示例和我的答案。

The implementation of display iterates through the backing array from 0 until item_num, so it prints the wrong values. It should iterate from front to rear instead (with checks for wrapping the index around at the end of the buffer) to print out the actual contents of the queue:

public void display() {
  int j = front;
  for (int count = 0; count < item_num; count++) {
    System.out.print(dqarr[j] + "  ");
    j = increment(j);
  }
  System.out.println();
}

private int increment(int index) {
  if (index == fullsize - 1)
    return 0;
  return ++index;
}

I defined an increment method to implement the index stepping in one single place - you have this in multiple places within your code.

Further note that there is no overflow check in your implementation: if I add fullsize + 1 elements to the queue, the first element will be silently overwritten.

Update: I realized that in your queue implementation rear indexes the last element of the queue (not the one after the last element, as is usual in dequeue implementations I have seen), so I modified the code example and my answer accordingly.

滿滿的愛 2024-09-09 13:56:47

如果这不是家庭作业,并且您使用的是 Java 6 或更高版本,为什么不直接使用内置的 双端队列

它们由 ArrayDeque< 组成/a>, LinkedList (在 Java 6 中使用此接口进行改造),以及并发 LinkedBlockingDeque

If this is not homework and you're using Java 6 or newer, why not just use on the built-in Deques?

Those consist of ArrayDeque, LinkedList (retrofitted with this interface in Java 6), and the concurrent LinkedBlockingDeque.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文