显示和插入方法错误(双端队列)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑最让您困惑的是您的
display
方法...:在这种方法中,您完全忽略了您编码的队列的“内部逻辑结构”,这主要取决于
front
和rear
索引:事实上,请注意,您甚至没有在这里提及这两个索引 - 您只需打印“物理上第一个”item_num
元素,与“逻辑上存在”的元素没有任何关系。相反,您需要从第
front
个元素(不是第0
个元素!)开始并显示item_num 元素从那里开始(当然,如果你遇到它,最后会环绕)。这将显示队列的逻辑内容,而不是实现它的数组的“随机”切片!-)
I suspect that's what confusing you most is your
display
method...:in this method you're completely ignored the "internal logical structure" of the queue you have coded, which depends crucially on the
front
andrear
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
front
th element (not the0
th one!) and show theitem_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!-)display
的实现从 0 到item_num
迭代支持数组,因此它打印了错误的值。它应该从front
到rear
迭代(检查是否将索引环绕在缓冲区末尾)以打印出实际内容队列的:我定义了一种
increment
方法来在一个位置实现索引步进 - 您可以在代码中的多个位置使用它。还要注意,您的实现中没有溢出检查:如果我将 fullsize + 1 元素添加到队列中,第一个元素将被静默覆盖。
更新:我意识到在您的队列实现中
rear
对队列的最后一个元素进行索引(而不是像往常一样在最后一个元素之后索引在我见过的出队实现中),所以我相应地修改了代码示例和我的答案。The implementation of
display
iterates through the backing array from 0 untilitem_num
, so it prints the wrong values. It should iterate fromfront
torear
instead (with checks for wrapping the index around at the end of the buffer) to print out the actual contents of the queue: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.如果这不是家庭作业,并且您使用的是 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
Deque
s?Those consist of
ArrayDeque
,LinkedList
(retrofitted with this interface in Java 6), and the concurrentLinkedBlockingDeque
.