Uva Wellassa University
Faculty of Technological Studies
BICT Degree Program
ICT 143-2 Data Structures and Algorithms
Practical 3 – Queue Implementation
class Queue {
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
public Queue(int s){ // constructor
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void insert(long j) { // put item at rear of queue
if(rear == maxSize-1){ // deal with wraparound
rear = -1;
}
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
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(){ //the queue is empty
return(nitems==0);
}
public int size(){ //number of items in the queue
return nitems;
}
1
} // end main ()
} // end class QueueApp
Exercise
1. Write a main method for the above program to hold 5 items in the queue
a. Insert 10,20,30,40 into the queue
b. Remove 10,20,30
c. Insert 4 items50,60,70 and 80
d. Display all items
2. Consider the following queue of characters where queue is a circular array of six memory sets
Front points to the 3rd position in the array
Rear points to the 5th position in the array
a. Insert A, B, C
b. Insert D
c. Remove A
d. Insert E, F &G
e. Remove B