Skip to content

Commit 185124a

Browse files
committed
Added Queues and Priority Queues
1 parent 816116e commit 185124a

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

Data_Structures/PriorityQueues.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* A priority queue adds elements into positions based on their priority.
3+
* So the most important elements are placed at the front/on the top.
4+
* In this example I give numbers that are bigger, a higher priority.
5+
* Queues in theory have no fixed size but when using an array implementation it does.
6+
*/
7+
class PriorityQueue{
8+
private int maxSize;
9+
private int[] queueArray;
10+
private int nItems;
11+
12+
public PriorityQueue(int size){ //Constructor
13+
maxSize = size;
14+
queueArray = new int[size];
15+
nItems = 0;
16+
}
17+
18+
public void insert(int value){ //Inserts an element in it's appropriate place
19+
if(nItems == 0){
20+
queueArray[0] = value;
21+
}
22+
else{
23+
int j = nItems;
24+
while(j > 0 && queueArray[j-1] > value){
25+
queueArray[j] = queueArray[j-1];
26+
j--;
27+
}
28+
queueArray[j] = value;
29+
}
30+
nItems++;
31+
}
32+
33+
public int remove(){ //Remove the element from the front of the queue
34+
return queueArray[--nItems];
35+
}
36+
37+
public int peek(){ //Checks what's at the front of the queue
38+
return queueArray[nItems-1];
39+
}
40+
41+
public boolean isEmpty(){ //Returns true is the queue is empty
42+
return(nItems == 0);
43+
}
44+
45+
public boolean isFull(){ //Returns true is the queue is full
46+
return(nItems == maxSize);
47+
}
48+
49+
public int getSize(){ //Returns the number of elements in the queue
50+
return nItems;
51+
}
52+
}
53+
//Example
54+
public class PriorityQueues{
55+
public static void main(String args[]){
56+
PriorityQueue myQueue = new PriorityQueue(4);
57+
myQueue.insert(10);
58+
myQueue.insert(2);
59+
myQueue.insert(5);
60+
myQueue.insert(3);
61+
//[2, 3, 5, 10] Here higher numbers have higher priority, so they are on the top
62+
63+
for(int i = 3; i>=0; i--)
64+
System.out.print(myQueue.remove() + " "); //will print the queue in reverse order [10, 5, 3, 2]
65+
66+
//As you can see, a Priority Queue can be used as a sorting algotithm
67+
}
68+
}

Data_Structures/Queues.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* A queue data structure functions the same as a real world queue.
3+
* The elements that are added first are the first to be removed.
4+
* New elements are added to the back/rear of the queue.
5+
*/
6+
class Queue{
7+
private int maxSize;
8+
private int[] queueArray;
9+
private int front;
10+
private int rear;
11+
private int nItems;
12+
13+
public Queue(int size){ //Constructor
14+
maxSize = size;
15+
queueArray = new int[size];
16+
front = 0;
17+
rear = -1;
18+
nItems = 0;
19+
}
20+
21+
public boolean insert(int x){ //Inserts an element at the rear of the queue
22+
if(isFull())
23+
return false;
24+
if(rear == maxSize-1) //If the back of the queue is the end of the array wrap around to the front
25+
rear = -1;
26+
rear++;
27+
queueArray[rear] = x;
28+
nItems++;
29+
return true;
30+
}
31+
32+
public int remove(){ //Remove an element from the front of the queue
33+
if(isEmpty()){
34+
System.out.println("Queue is empty");
35+
return -1;
36+
}
37+
int temp = queueArray[front];
38+
front++;
39+
if(front == maxSize) //Dealing with wrap-around again
40+
front = 0;
41+
nItems--;
42+
return temp;
43+
}
44+
45+
public int peekFront(){ //Checks what's at the front of the queue
46+
return queueArray[front];
47+
}
48+
49+
public int peekRear(){ //Checks what's at the rear of the queue
50+
return queueArray[rear];
51+
}
52+
53+
public boolean isEmpty(){ //Returns true is the queue is empty
54+
return(nItems == 0);
55+
}
56+
57+
public boolean isFull(){ //Returns true is the queue is full
58+
return(nItems == maxSize);
59+
}
60+
61+
public int getSize(){ //Returns the number of elements in the queue
62+
return nItems;
63+
}
64+
}
65+
//Example
66+
public class Queues{
67+
public static void main(String args[]){
68+
Queue myQueue = new Queue(4);
69+
myQueue.insert(10);
70+
myQueue.insert(2);
71+
myQueue.insert(5);
72+
myQueue.insert(3);
73+
//[10(front), 2, 5, 3(rear)]
74+
75+
System.out.println(myQueue.isFull()); //Will print true
76+
77+
myQueue.remove(); //Will make 2 the new front, making 10 no longer part of the queue
78+
//[10, 2(front), 5, 3(rear)]
79+
80+
myQueue.insert(7); //Insert 7 at the rear which will be index 0 because of wrap around
81+
// [7(rear), 2(front), 5, 3]
82+
83+
System.out.println(myQueue.peekFront()); //Will print 2
84+
System.out.println(myQueue.peekRear()); //Will print 7
85+
}
86+
}

0 commit comments

Comments
 (0)