user5232297 user5232297 - 1 month ago 8
Java Question

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

Here is my code where I'm attempting to create code for methods that will act as the deque in java
I have the methods as follows:


  • void deque();

  • void addFront();

  • void addRear();

  • void RemoveFront();

  • void RemoveRear();

  • void isempty();

  • void size();

  • void displayArray();



I have already managed to make the code for add front and I was wondering if anybody of you could help me in coding for
addRear()
,
RemoveFront()
and also
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

}

Answer

Begin with initializing CurrentFrontIndex and CurrentRearIndex to -1 since the (de)queue is empty in the beginning.

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
}

PLEASE NOTE: Even though I answered this question only to help you out, you are new to this site and just don't know the norms for asking questions here. Please check the following links and follow the regulations of this website next time onwards for the sake of your own reputation.

Tour - Stack Overflow
How do I ask a question
Writing the perfect question
How to ask questions the smart way

I'd want you to acknowledge that this question of yours is of a very poor quality and almost unsalvageable. If you continue to ask such kind of questions, you can face a question ban.

Comments