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
爲您重新設置了代碼(仍然是一團糟,但比以前更糟糕),並添加了明顯缺少的java標記(顯示標記絕對沒有任何意義,但我不明白您的意思足以幫助)。我建議進一步編輯和標記說明,以使這個問題不再是一場災難,目前它仍然是, – 2010-05-23 15:14:22
thanxs。 我有問題在顯示方法 我從左邊刪除,但當我調用顯示刪除後,我發現從右邊的元素它是刪除不剩^^ – MANAL 2010-05-23 16:38:27
在這個問題* * * *和* ??? *太多的哭泣。 – whiskeysierra 2010-05-23 22:25:21