这是我的代码,我正在尝试为将充当java中的deque的方法创建代码 我有如下方法:
void deque();
void addFront();
void addRear();
void RemoveFront();
void RemoveRear();
void isempty();
void size();
void displayArray();
我已经设法制作了添加前置的代码,我想知道你是否有人可以帮助我编写addRear()
,RemoveFront()
和RemoveRear()
的编码。
import java.util.Scanner;
public class DequeMethods implements Deque{
int array [];
int limit;
int CurrentFrontIndex=0;
int CurrentRearIndex;
Scanner in = new Scanner(System.in);
@Override
public void deque() {
// TODO Auto-generated method stub
System.out.println("input deque limit");
this.limit = in.nextInt();
array = new int [limit];
for(int x = 0; x<limit; x++){
array[x]=0;
}
}
@Override
public void addFront() {
// TODO Auto-generated method stub
boolean Itemfull= false;
for(int x=0; x<limit;x++){
if (array[x]==0){
Itemfull= false;
CurrentFrontIndex = x;
break;
}else{
Itemfull=true;}
if(Itemfull=true){
System.out.println("input int value");
int value = in.nextInt();
int y;
for(y=CurrentFrontIndex; y>0;y--){
array[y] = array [y-1];
}
array [y]=value;
}
}
}
@Override
public void addRear() {
// TODO Auto-generated method stub
}
@Override
public void RemoveFront() {
// TODO Auto-generated method stub
}
@Override
public void RemoveRear() {
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
首先将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
}
addLast()
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
}
RemoveFront()
void RemoveFront(){
if(CurrentFrontIndex == CurrentRearIndex){
CurrentFrontIndex = -1;
CurrentRearIndex = -1;
}
else if(CurrentFrontIndex >= 0)
CurrentFrontIndex++;
else
//array is empty; cannot remove
}
void RemoveRear()
void RemoveRead(){
if(CurrentRearIndex == CurrentFrontIndex){
CurrentRearIndex = -1;
CurrentFrontIndex = -1;
}
else if(CurrentRearIndex <= array.length)
CurrentRearIndex--;
else
//array is empty; cannot remove
}
请注意:即使我回答这个问题只是为了帮助你,但你是这个网站的新手,并且不知道在这里提问的规范。请检查以下链接,并在下次开始时遵循本网站的规定,以保证您的声誉。
♦Tour - Stack Overflow
♦How do I ask a question
♦Writing the perfect question
♦How to ask questions the smart way
我希望你承认,你的这个问题质量很差,而且几乎无法弥补。如果您继续提出此类问题,可以面对question ban。