2010-05-23 59 views
1

1)我的問題錯誤顯示和插入方法(雙端隊列)

當我做由右刪除或左程序將被刪除真實 但是當我打電話diplay方法的內容錯誤

這樣 我插入物12 43 65 23 和時許從左程序刪除將刪除12 但當呼叫顯示方法顯示這樣12 43 65

和時許從右程序刪除將刪除23 但是當c所有顯示方法如下所示12 43

爲什麼?????? );

,當我試圖使刪除寫後插入,因爲隊列已滿這

不能正確插入。首先刪除權,那麼在哪裏插入右

哪裏出了問題?

請幫我

2)我的代碼

FIRST CLASS

class dqueue 
{ 
    private int fullsize;     //number of all cells 
    private int item_num;     // number of busy cells only 
    private int front,rear; 
    public int j; 
    private double [] dqarr; 
//========================================== 
    public dqueue(int s)      //constructor 
    { 
     fullsize = s; 
     front = 0; 
     rear = -1; 
     item_num = 0; 
     dqarr = new double[fullsize]; 
    } 
//========================================== 
    public void insert(double data) 
    { 
     if (rear == fullsize-1) 
      rear = -1; 
     rear++; 
     dqarr[rear] = data; 
     item_num++; 

    } 
    public double removeLeft() // take item from front of queue 
    { 
    double temp = dqarr[front++]; // get value and incr front 
    if(front == fullsize) 
    front = 0; 
    item_num --; // one less item 
    return temp; 
    } 
    public double removeRight() // take item from rear of queue 
    { 
    double temp = dqarr[rear--]; // get value and decr rear 
    if(rear == -1) // 
    rear = item_num -1; 
    item_num --; // one less item 
    return temp; 
    } 
//========================================= 

    public void display()    //display items 
{ 
for (int j=0;j<item_num;j++)    // for every element 
System.out.print(dqarr[j] +" ");   // display it 
System.out.println(""); 
} 

//========================================= 
public int size()    //number of items in queue 
{ 
return item_num; 
} 
//========================================== 
public boolean isEmpty()  // true if queue is empty 
{ 
return (item_num ==0); 
} 


} 

第二類

import java.util.Scanner; 
class dqueuetest 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println("    ***** Welcome here***** "); 
     System.out.println("    ***** Mind Of Programming Group***** "); 
     System.out.println("    _____________________________________________ "); 
     System.out.println("enter size of your dqueue"); 
     int size = input.nextInt(); 
     dqueue mydq = new dqueue(size); 
     System.out.println(""); 
     System.out.println("enter your itemes"); 
//===================================== 
     for(int i = 0;i<=size-1;i++) 
     { 
      System.out.printf("item %d:",i+1); 
      double item = input.nextDouble(); 
      mydq.insert(item); 
      System.out.println(""); 
     }  

//===================================== 
int queue =size ;   
int c = 0 ; 
while (c != 6) 
{ 
    System.out.println(""); 
    System.out.println("************************************************"); 
    System.out.println("    MAIN MENUE"); 
    System.out.println("1- INSERT RIGHT "); 
    System.out.println("2- REMOVE LEFT"); 
    System.out.println("3- REMOVE RIGHT"); 
    System.out.println("4- DISPLAY"); 
    System.out.println("5- SIZE"); 
    System.out.println("6- EXIT"); 
    System.out.println("************************************************"); 
    System.out.println("choose your operation by number(1-6)"); 
    c = input.nextInt(); 
    switch (c) 
    { 
    case 1: 
    if (queue == size) 
    System.out.print("Can not insert right because the queue is full . first remove right and then u can insert right "); 
      else { System.out.print("enter your item: "); 
      double item = input.nextDouble(); 
      mydq.insert(item);} 
      break; 

    case 2: 
     System.out.println("REMOVE FROM REAR :"); 
       if(!mydq.isEmpty()) 
    { 
    double item = mydq.removeLeft(); 

    System.out.print(item + "\t"); 
    } // end while 
    System.out.println(""); 
    mydq.display(); 

    break; 

    case 3: 
    System.out.println("REMOVE FROM FRONT :"); 
      if(!mydq.isEmpty()) 
    { 
    double item = mydq.removeRight(); 

    System.out.print(item + "\t"); 
    } // end while 
    System.out.println(""); 
    mydq.display(); 
    break; 

    case 4: 
    System.out.println("The items in Queue are :"); 
     mydq.display(); 
    break; 

    case 5: 
    System.out.println("The Size of the Queue is :"+mydq.size()); 
    break; 

    case 6: 
    System.out.println("Good Bye"); 

    break; 

    default: 
    System.out.println("wrong chiose enter again"); 
    }  //end switch 
}  //end while 
}  // end main  

}//end class 
+2

爲您重新設置了代碼(仍然是一團糟,但比以前更糟糕),並添加了明顯缺少的java標記(顯示標記絕對沒有任何意義,但我不明白您的意思足以幫助)。我建議進一步編輯和標記說明,以使這個問題不再是一場災難,目前它仍然是, – 2010-05-23 15:14:22

+0

thanxs。 我有問題在顯示方法 我從左邊刪除,但當我調用顯示刪除後,我發現從右邊的元素它是刪除不剩^^ – MANAL 2010-05-23 16:38:27

+0

在這個問題* * * *和* ??? *太多的哭泣。 – whiskeysierra 2010-05-23 22:25:21

回答

1

我懷疑這是WH在迷惑你的就是你的display方法...:

public void display()    //display items 
{ 
    for (int j=0;j<item_num;j++)    // for every element 
     System.out.print(dqarr[j] +" ");   // display it 
    System.out.println(""); 
} 
在此方法中你完全忽略了你所編碼的隊列,這關鍵取決於 frontrear指數的「內部邏輯結構」

:的確,注意你甚至沒有提及這些指標無論是在這裏 - 你只是打印了「身體第一」 item_num元素,有任何與「邏輯上存在」的人沒有任何關係。

你需要,相反,與front個元素(0個一種!)啓動並顯示item_num元素開始出現(在最後與環繞如果你遇到它,當然)。這將顯示您的隊列的邏輯內容,而不是實現它的陣列的「隨機OID」片 - !)

+0

取悅更多解釋 ): – MANAL 2010-05-23 16:39:15

1

display遍歷從0支持數組,直到item_num的實現,所以它打印錯誤的價值觀。應該迭代從frontrear代替(與檢查周圍包裹索引處的緩衝區的末尾),以打印出隊列的實際內容:

public void display() { 
    int j = front; 
    for (int count = 0; count < item_num; count++) { 
    System.out.print(dqarr[j] + " "); 
    j = increment(j); 
    } 
    System.out.println(); 
} 

private int increment(int index) { 
    if (index == fullsize - 1) 
    return 0; 
    return ++index; 
} 

我所定義的increment方法來實現索引步進一個地方 - 你在你的代碼中有多個地方。

另外請注意,在您的實現中沒有溢出檢查:如果我將fullsize + 1元素添加到隊列中,則第一個元素將被自動覆蓋。

更新:我意識到,在你的隊列實現rear指標隊列的最後一個元素(不是的最後一個元素之後的一個,如在出隊實現我所看到的通常的),所以我修改了代碼例如和我的答案相應。

+0

仍有問題 當我用你的方法顯示使從刪除這兩個右和後方 :(:( 這樣 我插入12 34 45 67 並做出從左 刪除了放變成這樣34 45 ):): 請幫我 – MANAL 2010-05-23 16:35:18

+0

@MANAL,看我的更新。 – 2010-05-24 19:19:28