Repository navigation
Expand file tree
/
Copy pathQueue.java
More file actions
119 lines (103 loc) · 2.89 KB
/
Copy pathQueue.java
File metadata and controls
119 lines (103 loc) · 2.89 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*implementation of Queue and Priority Queue using Arrays*/
/*
Arrays, linked lists, trees, etc. are best for
data that represents real objects.
Stacks & Queues are instead used to complete a
task and are soon after discarded.
Stacks & Queues
1. Allow only a single item to be added or removed at a time
2. Queues allow access to the first item inserted (FIFO)
*/
public class Queue {
private String[] queueArray;
private int queueSize;
//initializing queue as empty
private int front, rear, numberOfItems = 0;
public Queue(int size){
queueSize = size;
queueArray = new String[queueSize];
}
public void insert(String value){
if (numberOfItems + 1 <= queueSize){
queueArray[rear] = value;
rear += 1;
numberOfItems += 1;
System.out.println(String.format("Insert : value %s was added to queue", value));
} else{
System.out.println("Sorry Queue is full.");
}
}
public void insert_many(String[] values){
for (String value : values){
insert(value);
}
}
public void remove(){
if( numberOfItems >0 ){
String value = queueArray[front];
queueArray[front] = null;
front += 1;
numberOfItems -= 1;
System.out.println(String.format("Remove : value %s was removed from queue", value));
} else {
System.out.println("Sorry Queue is already empty");
}
}
public void peek(){
if (numberOfItems > 0 ){
System.out.println(String.format("Peek : value %s is in front of queue", queueArray[front]));
} else {
System.out.println("Sorry Queue is already empty");
}
}
// This priority insert will add items in order from high to low
public void priorityInsert(String input){
int i;
if(numberOfItems == 0){
insert(input);
} else {
for(i = numberOfItems-1; i >= 0; i--){
if(Integer.parseInt(input) > Integer.parseInt(queueArray[i])){
queueArray[i+1] = queueArray[i];
} else break; // Done shifting items in queue
}
queueArray[i+1] = input;
rear += 1;
numberOfItems += 1;
}
}
public void priorityInsertMany(String[] values){
for (String value : values){
priorityInsert(value);
}
}
public void displayQueue(){
System.out.print("[ ");
for( String value : queueArray){
if (value != null){
if (value == queueArray[front] && value == queueArray[rear]) {
System.out.print(value + " [F] [R],");
} else if ( value == queueArray[front]){
System.out.print(value + " [F],");
} else if (value == queueArray[rear - 1]){
System.out.print(value + " [R],");
} else {
System.out.print(value + " ,");
}
} else{
System.out.print(" ,");
}
}
System.out.print(" ]\n");
}
public static void main(String [] args){
Queue theQueue = new Queue(10);
theQueue.priorityInsert("16");
theQueue.priorityInsert("25");
theQueue.priorityInsert("10");
theQueue.displayQueue();
theQueue.remove();
theQueue.peek();
theQueue.displayQueue();
}
}