Java 如何定义从后面和前面添加和删除元素的出列方法?
这是我的代码,我试图为在java中充当deque的方法创建代码 我的方法如下:Java 如何定义从后面和前面添加和删除元素的出列方法?,java,arrays,deque,arraydeque,Java,Arrays,Deque,Arraydeque,这是我的代码,我试图为在java中充当deque的方法创建代码 我的方法如下: void-deque() void addFront() void addRear() void RemoveFront() void-rear() void isempty() void size() void displayArray() 我已经成功地编写了add front的代码,我想知道你们中是否有人可以帮助我编写addRear(),RemoveFront(),以及removeear() import j
void-deque()代码>
void addFront()代码>
void addRear()代码>
void RemoveFront()代码>
void-rear()代码>
void isempty()代码>
void size()代码>
void displayArray()代码>
addRear()
,RemoveFront()
,以及removeear()
import java.util.Scanner;
公共类DequeMethods实现Deque{
int数组[];
整数极限;
int CurrentFrontIndex=0;
国际货币指数;
扫描仪输入=新扫描仪(系统输入);
@凌驾
公共空间{
//TODO自动生成的方法存根
System.out.println(“输入偏差限制”);
this.limit=in.nextInt();
数组=新整数[限制];
对于(int x=0;x而言,首先将CurrentFrontIndex
和CurrentRearIndex
初始化为-1
,因为(de)队列在开始时是空的
addFirst()
void addFirst(int a){
if(CurrentFrontIndex == -1){
array[++CurrentFrontIndex] = a;
CurrentRearIndex++;
}
else if(CurrentFrontIndex > 0)
array[--CurrentFrontIndex] = a;
else
//cannot add to front
}
void addRear(int a){
if(CurrentRearIndex == -1){
array[++CurrentRearIndex] = a;
CurrentFrontIndex++;
}
else if(CurrentRearIndex < array.length - 1)
array[++CurrentRearIndex] = a;
else
//cannot at to rear
}
void RemoveFront(){
if(CurrentFrontIndex == CurrentRearIndex){
CurrentFrontIndex = -1;
CurrentRearIndex = -1;
}
else if(CurrentFrontIndex >= 0)
CurrentFrontIndex++;
else
//array is empty; cannot remove
}
void RemoveRead(){
if(CurrentRearIndex == CurrentFrontIndex){
CurrentRearIndex = -1;
CurrentFrontIndex = -1;
}
else if(CurrentRearIndex <= array.length)
CurrentRearIndex--;
else
//array is empty; cannot remove
}
addLast()
void addFirst(int a){
if(CurrentFrontIndex == -1){
array[++CurrentFrontIndex] = a;
CurrentRearIndex++;
}
else if(CurrentFrontIndex > 0)
array[--CurrentFrontIndex] = a;
else
//cannot add to front
}
void addRear(int a){
if(CurrentRearIndex == -1){
array[++CurrentRearIndex] = a;
CurrentFrontIndex++;
}
else if(CurrentRearIndex < array.length - 1)
array[++CurrentRearIndex] = a;
else
//cannot at to rear
}
void RemoveFront(){
if(CurrentFrontIndex == CurrentRearIndex){
CurrentFrontIndex = -1;
CurrentRearIndex = -1;
}
else if(CurrentFrontIndex >= 0)
CurrentFrontIndex++;
else
//array is empty; cannot remove
}
void RemoveRead(){
if(CurrentRearIndex == CurrentFrontIndex){
CurrentRearIndex = -1;
CurrentFrontIndex = -1;
}
else if(CurrentRearIndex <= array.length)
CurrentRearIndex--;
else
//array is empty; cannot remove
}
void removeer()
void addFirst(int a){
if(CurrentFrontIndex == -1){
array[++CurrentFrontIndex] = a;
CurrentRearIndex++;
}
else if(CurrentFrontIndex > 0)
array[--CurrentFrontIndex] = a;
else
//cannot add to front
}
void addRear(int a){
if(CurrentRearIndex == -1){
array[++CurrentRearIndex] = a;
CurrentFrontIndex++;
}
else if(CurrentRearIndex < array.length - 1)
array[++CurrentRearIndex] = a;
else
//cannot at to rear
}
void RemoveFront(){
if(CurrentFrontIndex == CurrentRearIndex){
CurrentFrontIndex = -1;
CurrentRearIndex = -1;
}
else if(CurrentFrontIndex >= 0)
CurrentFrontIndex++;
else
//array is empty; cannot remove
}
void RemoveRead(){
if(CurrentRearIndex == CurrentFrontIndex){
CurrentRearIndex = -1;
CurrentFrontIndex = -1;
}
else if(CurrentRearIndex <= array.length)
CurrentRearIndex--;
else
//array is empty; cannot remove
}
void-removereead(){
如果(CurrentRearIndex==CurrentFrontIndex){
CurrentRearIndex=-1;
CurrentFrontIndex=-1;
}
否则,如果(CurrentRearIndex)在当前看起来更像是一个咆哮而不是一个问题,请提出具体问题,更清楚地解释您的问题。您可以检查-