Write a method for the Queue class in the queue.java program(Listing 4.4) that displays the contents of
Question:
Write a method for the Queue class in the queue.java program(Listing 4.4) that displays the contents of the queue. Note thatthis does not mean simply displaying the contents of the underlyingarray. You should show the queue contents from the first iteminserted to the last, without indicating to the viewer whether thesequence is broken by wrapping around the end of the array. Becareful that one item and no items display properly, no matterwhere front and rear are.
Listing 4.4 is below
class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//
public Queue(int s)
{
maxSize = s;
queArray = new long[maxSize];
front =0;
rear = -1;
nItems = 0;
}
//
public void insert(long j)
{
if(rear == maxSize -1)
rear = -1;
queArray[++rear] = j;
nItems++;
}
//
public long remove()
{
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
//
public long peekFront()
{
return queArray[front];
}
//
public boolean isEmpty()
{
return(nItems==0);
}
//
public boolean isFull()
{
return (nItems==maxSize);
}
//
public int size()
{
return nItems;
}
//
} //end class
class QueueApp
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while( !theQueue.isEmpty() )
{
long n = theQueue.remove();
System.out.print(n);
System.out.print( " ");
}
System.out.println(" ");
} //end main()
} //end class
4.2
Create a Deque class based on the discussion of deques(double-ended queues) in this chapter. It should includeinsertLeft(), insertRight(), removeLeft(), removeRight(),isEmpty(), and isFull() methods. It will need to support wraparoundat the end of the array, as queues do.
4.3
Write a program that implements a stack class that is based onthe Deque class in the Programming Project 4.2. This stack classshould have the same methods and capabillities as the StackX classin the stack.java program (Listing 4.1).
Listing 4.1 is below
class StackX
{
private int maxSize;
private long[] stackArray;
private int top;
//
public stackX(int s)
{
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
//
public void push(long j)
{
stackArray[++top] = j;
}
//
public long pop()
{
return stackArray[top --];
}
//
public long peek()
{
return stackArray[top];
}
//
public boolean isEmpty()
{
return (top == -1);
}
//
public boolean isFull()
{
return (top == maxSize-1);
}
//
} //end class StackX
class StackApp
{
public static void main(String[] args)
{
StackX the Stack = new StackX(10);
theStack.push(20);
theStack.push(40);
theStack.push(60);
theStack.push(80);
while( !theStack.isEmpty() )
{
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
} //end while
System.out.println(" ");
} //end main
} //end class
Fundamentals of Analytical Chemistry
ISBN: 978-0495558286
9th edition
Authors: Douglas A. Skoog, Donald M. West, F. James Holler, Stanley R. Crouch