到目前为止,这一切都(至少我希望如此笑)是队列类/为IsEmpty方法
class Queue
{
private int front = 0; //Creates front and back int holders and an array
private int back = -1;
private int[] anArray;
public Queue(int size) // constructor to get the Queue size
{
anArray = new int[size];
}
public bool IsFull
{
get // gets to see if the Queue is full (I assume I did this right, It's full if the back of the queue is equal to -1 of an array)
{
return back == anArray.Length - 1;
}
}
public bool IsEmpty
{
get // It's empty if the back is -1; I think this is where I messed up, I think that when it checks to see if it's empty it also doesn't check if it's empty when I have dequeued the other numbers (or write them off). Would I make something like "Return front == anArray.Length -1;" ? That would check to see when the front (The part being written to console first) hits the end of the array?
{
return back == -1;
}
}
public void Enqueue(int valueEnqueue)
{ // Method to Enqueue the variables into the array
if (IsFull)
{
//do nothing
}
else
{
back = back + 1;
anArray[back] = valueEnqueue;
}
}
public int Dequeue()
{ // Method to dequeue the variables put in
if (IsEmpty)
{
//do nothing
return 1010101010;
}
else
{
int dequeue = anArray[front];
front = front + 1;
return dequeue;
}
}
所以我想我的问题是什么,通过正常的队列思维守法(先入先出)如何我能让它停下来吗?我一直在索引超出范围错误。
140列的行不太可读。请分解它。 – 2011-04-10 19:22:04
这是如何不可读的?我跳过了几行,等等。 – Nogg 2011-04-10 19:22:43
你不是很接近,这比堆栈更像堆栈。你必须使用[家庭作业]标签来获得你需要的帮助。 – 2011-04-10 19:23:21