如何定义 Dequeue 方法来从前后添加和删除元素?

How to define Dequeue methods to add and remove elements from rear and front?

这是我的代码,我在其中尝试为将充当 java 中的双端队列的方法创建代码 我有如下方法:

我已经设法编写了添加前端的代码,我想知道你们中是否有人可以帮助我编写 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

}

开始将 CurrentFrontIndexCurrentRearIndex 初始化为 -1,因为(去)队列一开始是空的。

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.