Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment #2
- public class Solver
- {
- public static void main(String args[])
- {
- System.out.println();
- Circular_priority_queue CPQ = new Circular_priority_queue(5);
- CPQ.push(5);
- System.out.printf("-----\nThe Top is: %d\n-----\n", CPQ.top());
- CPQ.push(3);
- System.out.printf("-----\nThe Top is: %d\n-----\n", CPQ.top());
- CPQ.push(5);
- System.out.printf("-----\nThe Top is: %d\n-----\n", CPQ.top());
- CPQ.push(1);
- System.out.printf("-----\nThe Top is: %d\n-----\n", CPQ.top());
- CPQ.pop();
- System.out.printf("-----\nThe Top is: %d\n-----\n", CPQ.top());
- System.out.println();
- }
- }
- class Circular_priority_queue
- {
- int rear, front, MAXSIZE, arr[], nItems;
- Circular_priority_queue(int size)
- {
- MAXSIZE = size;
- rear = -1;
- front = nItems = 0;
- arr = new int[MAXSIZE];
- }
- void push(int val)
- {
- if (isFull() == true)
- {
- System.out.println("Sorry, The Queue is Full");
- return;
- }
- int i = rear, cnt = 1;
- while (cnt <= nItems && val < arr[i])
- {
- arr[(i + 1) % MAXSIZE] = arr[i];
- i = (i - 1 + MAXSIZE) % MAXSIZE;
- cnt++;
- }
- arr[(i + 1) % MAXSIZE] = val;
- rear = (rear + 1) % MAXSIZE;
- nItems++;
- }
- int pop()
- {
- if (isEmpty())
- {
- System.out.println("The Queue is Empty");
- return -1;
- }
- int temp = arr[front];
- front = (front + 1) % MAXSIZE;
- nItems--;
- return temp;
- }
- int top()
- {
- if (isEmpty())
- {
- System.out.println("The Queue is Empty");
- return -1;
- }
- return arr[front];
- }
- boolean isEmpty()
- {
- return nItems == 0;
- }
- boolean isFull()
- {
- return nItems == MAXSIZE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment