Академический Документы
Профессиональный Документы
Культура Документы
Laboratory Exercise 6
Queues
At the end of the exercise, the students should be able to:
Identify how queues are used as data structure
Debug the given program to obtain the required outputs
Materials:
PC with JCreator or any Java Editor
3 floppy disk
Activity1
Given the program below, debug and modify the application to acquire the following
outputs:
o 36.0 98.0 546.0 687.0 957.0
o 546.0 687.0 36.0 98.0 957.0
class PriorityQ
{
private int maxSize;
private double[] queArray;
private int nItems;
//-----------------------------------------------------------public Priority(int s)
{
maxSize = s;
queArray = new double[maxsize];
nItems = 0;
}
//----------------------------------------------------------public void insert(double item)
{
int j;
if(nItems==0)
queArray[nItems++] = item;
else
{
for(j=nItems-1; j>=0; j--)
{
if(item > queArray[j])
queArray[i+1] = item;
else
break;
}
queArray[j+1] = item;
nItems--;
}
}
//----------------------------------------------------------public double remove()
{
return queArray[--nItems];
}
//----------------------------------------------------------public double peekMin()
{
return queArray[nItems-1];
}
Laboratory Exercise 6
*Property of STI
Page 1 of 4
Data Structures
Activity2
Given the
outputs:
o 40
o 70
o 80
o 40
o 50
program below, debug and modify the application to acquire the following
50
80
40
50
60
60
10
50
60
70
70
20
60
70
80
80
60
70 80
80 40
40
import java.io.*;
class Queue
{
private int[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------public Queue(int s)
{
maxSize = s;
queAray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------Laboratory Exercise 6
*Property of STI
Page 2 of 4
Data Structures
*Property of STI
Page 3 of 4
Data Structures
{
int n = theQueue.remove();
System.out.print(n);
}
System.out.println(" ");
}
}
Activity3
Explain on the space provided below what the two activities do.
Laboratory Exercise 6
*Property of STI
Page 4 of 4