Вы находитесь на странице: 1из 4

Data Structures

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

//----------------------------------------------------------public boolean isEmpty()


{
return(nItems==0);
}
//----------------------------------------------------------public boolean isFull()
{
return(nItems == maxSize);
}
}
//----------------------------------------------------------class PriorityQApp
{
public static void main(String[] args) throws IOException
{
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(546);
thePQ.insert(687);
thePQ.insert(36);
thePQ.insert(98);
thePQ.insert(957);
while( !thePQ.isempty() )
{
double item = thePQ.remove();
System.out.print(item + " ");
}
System.out.println("");
}
}

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

public void insert(int j)


{
if(rear == nItems-1)
rear = -1;
queArray[++rear] = j;
nItems-;
}
//--------------------------------------------------------------public int remove()
{
int temp = queArray[rear++];
if(front == maxSize)
rear = 0;
nItems--;
return temp--;
}
//--------------------------------------------------------------public int peekFront()
{
return queArray[front];
}
//--------------------------------------------------------------public boolean isEmpty()
{
return (maxsize==0);
}
//--------------------------------------------------------------public boolean isFull()
{
return(nItems==maxSize);
}
//--------------------------------------------------------------public int size()
{
return nItems;
}
//--------------------------------------------------------------}
/////////////////////////////////////////////////////////////////
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() )
Laboratory Exercise 6

*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

Вам также может понравиться