Thursday, April 2, 2015

Java Deque problem


Hey all,


This is a homework assignment regarding deques. I have it all finished and working with my inserts going from all left or all right but it doesn't work when I do both. I get this weird zero popping up and I have no clue as to what I am missing. Ive shown others in my class and they don't know. Thanks for any tips in advance!



Java Code:



//demonstrates queue
//to run this program: C>java QueueApp
////////////////////////////////////////////////////////////////

package project2;

class deque {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;

// --------------------------------------------------------------
public deque(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = maxSize-1;
nItems = 0;
}

// --------------------------------------------------------------
public void insertfront(long j) // put item at rear of queue
{
if (isFull()){
System.out.println("Full cannot insert!");
}
if (front == 0) // deal with wraparound
front = maxSize-1;
queArray[--front] = j; // increment rear and insert
nItems++; // one more item
}

// --------------------------------------------------------------
public void insertrear(long j) // put item at rear of queue
{
if (isFull()){
System.out.println("Full cannot insert!");
}
if (rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}

// --------------------------------------------------------------
public long removefront() // take item from front of queue
{
if (isEmpty()){
System.out.println("Empty!");
}
long temp = queArray[front++];// get value and incr front
if (front == maxSize-1) // deal with wraparound
front = -1;
nItems--; // one less item
return temp;
}

// --------------------------------------------------------------
public long removerear() // take item from front of queue
{
if (isEmpty()){
System.out.println("Empty!");
}
long temp = queArray[rear--];// get value and incr front
if (rear < 0) // deal with wraparound
rear = maxSize-1;
nItems--; // one less item
return temp;
}
//----------------------------------------------------------------
public long remove() // take item from front of queue
{
long temp = queArray[front++]; // get value and incr front
if (front == maxSize) // deal with wraparound
front = 0;
nItems--; // one less item
return temp;
}

// --------------------------------------------------------------
public long peekFront() // peek at front of queue
{
return queArray[front];
}

// --------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems == 0);
}

// --------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems == maxSize);
}

// --------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
// --------------------------------------------------------------
public void display() // displays array contents
{
for (int j = 0; j < nItems; j++) {
long dis=queArray[(front + j) % maxSize];
System.out.print(dis);
System.out.print(" ");
}
System.out.println("");
}
//---------------------------------------------------------------
} // end class Queue
////////////////////////////////////////////////////////////////

class QueueApp {
public static void main(String[] args) {
deque theQueue = new deque(5); // queue holds 5 items

theQueue.insertrear(10); // insert 4 items
theQueue.display();
theQueue.insertrear(20);
theQueue.display();
theQueue.insertrear(30);
theQueue.display();
theQueue.insertfront(40);
theQueue.display();

/*theQueue.removefront(); // remove 3 items
theQueue.display();
theQueue.removefront(); // (10, 20, 30)
theQueue.display();
theQueue.removefront();
theQueue.display();

theQueue.insertfront(50); // insert 4 more items
theQueue.display();
theQueue.insertfront(60); // (wraps around)
theQueue.display();
theQueue.insertfront(70);
theQueue.display();
theQueue.insertfront(80);
theQueue.display();*/

while (!theQueue.isEmpty()) // remove and display
{ // all items
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
} // end main()
} // end class QueueApp
////////////////////////////////////////////////////////////////

The output that I get when I do insert front, front, front, rear:

10

10 20

10 20 30

40 0 10 20

40 0 10 20

No comments:

Post a Comment