-
Notifications
You must be signed in to change notification settings - Fork 8
/
PriorityQueue.java
90 lines (89 loc) · 1.97 KB
/
PriorityQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//*******************************************************
// PriorityQueue.java
// A class that represents an implementation of PriorityQueue in java
// Author: liron mizrahi
//*******************************************************
public class PriorityQueue
{
private int _max;
private int[] _arr;
private int _length;
/**
* constructor of the class
* @param int
* @return None
*/
public PriorityQueue(int max)
{
_max = max;
_arr = new int[_max];
_length = 0;
}// end of method PriorityQueue
/**
* insert method will insert data to the priority queue
* @param int
* @return None
*/
public void insert(int val)
{
int i;
if (_length == 0)
{
_arr[0] = val;
_length++;
return;
}
for(i = _length - 1; i >= 0; i--)
{
if(val > _arr[i])
{
_arr[i + 1] = _arr[i];
}
else
{
break;
}
}
_arr[i + 1] = val;
_length++;
}// end of method insert
/**
* printPriorityQueue method will print the values in the queue
* @param None
* @return None
*/
public void printPriorityQueue()
{
for( int i = 0; i < _length; i++)
{
System.out.print(_arr[i] + " ");
}
}// end of method printPriorityQueue
/**
* remove method remove the last value in the queue
* @param None
* @return int
*/
public int remove()
{
return _arr[--_length];
}// end of method remove
/**
* isFull method return if the queue is full or not
* @param None
* @return boolean
*/
public boolean isFull()
{
return _length == _max;
}// end of method isFull
/**
* isEmpty method return if the queue is empty or not
* @param None
* @return boolean
*/
public boolean isEmpty()
{
return _length == 0;
}// end of method isEmpty
}// end of class PriorityQueue