2016-07-27 2 views
-3

Вот мой код, где я пытаюсь создать код для методов, которые будут действовать как дека в Java у меня есть методы следующим образом:Как определить методы Dequeue для добавления и удаления элементов сзади и спереди?

  • 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 

} 
+1

В настоящее время это скорее похоже на напыщенный вопрос, чем вопрос, пожалуйста, задайте конкретный вопрос, объясняющий, что является вашей проблемой более четко. –

+0

Вы можете проверить - http://www.sanfoundry.com/java-program-array-deque/ –

ответ

0

Начните с инициализацией CurrentFrontIndex и CurrentRearIndex к -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 
} 

пустота 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.

+0

Извините и спасибо, что указали это – user5232297

+0

@ user5232297 Было ли решение работать на вас? – progyammer

+0

да, это было хорошо – user5232297

Смежные вопросы