2016-07-27 55 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

與初始化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 
} 

無效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

相關問題