// moves all the elements between start and end including start and
end to the index of newStart and moves the element in newStart to
after the element of end,
Queue x = {1, 2, 3, 4, 5, 6}; moveTo(x, 1, 3, 0) // can’t move to
anywhere between
1=<newStart<3
Here we chose the indexes 1-3 which is {2, 3, 4} to move to index 0
which is {1}
So after using the method x will be: x = {2, 3, 4, 1, 5, 6}
Let x = {1, 2, 3, 4, 5, 6}
To move the chosen indexes to the last index the newStart should be
equal to
((sizeQ(x)-1)-((end)- (start))) in this case 3 so to move {2, 3, 4} to the
last index the operation will look like this moveTo(x, 1, 3, 3) it’ll result
in x = {1, 5, 6, 2, 3, 4}
public static <T> void moveTo(Queue<T> q, int start, int end, int
newStart) {
if(newStart == sizeQ(q)){
if (start == end) {
return;
}
Queue<T> tempQueue = new Queue<>();
Queue<T> movedElements = new Queue<>();
for (int i = 0; i < start; i++) {
[Link]([Link]());
}
for (int i = start; i <= end; i++) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
}else {
Queue<T> temp = new Queue<>();
Queue<T> moved = new Queue<>();
int i = 0;
while (i < start) {
[Link]([Link]());
i++;
}
i = start;
while (i <= end) {
[Link]([Link]());
i++;
}
while (![Link]()) {
[Link]([Link]());
}
i = 0;
while (i < newStart) {
[Link]([Link]());
i++;
}
while (![Link]()) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
}
}
// creates a Queue of integers with the indexes at which there is val in
the Queue
x = {1, 2, 3, 4, 1}; Queue<Integer> y = OcValQ(x, 1); = {0, 4}
public static <T> Queue<Integer> OcValQ(Queue<T> q, T val) {
Queue<Integer> indices = new Queue<>();
Queue<T> tempQueue = copyQ(q);
int count = 0;
while (![Link]()) {
T element = [Link]();
if ([Link](val)) {
[Link](count);
}
count++;
}
return indices;
}
// removes every of the None first Occurrences of any value in a Q,
X = {1, 2, 3, 4, 1} removeNonFirstOccurrencesQ(x) results in x = {1, 2,
3, 4}
public static <T> void removeNonFirstOccurrencesQ(Queue<T>
queue) {
if ([Link]()) return;
Queue<T> uniqueElements = new Queue<>();
Queue<T> tempQueue = new Queue<>();
while (![Link]()) {
T current = [Link]();
if (!containsElementQ(uniqueElements, current)) {
[Link](current);
[Link](current);
}
}
while (![Link]()) {
[Link]([Link]());
}
}
// inserts chosen value at chosen index, x = {1, 2, 3, 4}; insertQ(x, 0,
4) results in
x = {4, 1, 2, 3, 4}
public static <T> void insertQ(Queue<T> queue, int index, T value) {
Queue<T> tempQueue = new Queue<>();
int currentIndex = 0;
boolean inserted = false;
while (![Link]()) {
if (currentIndex == index) {
[Link](value);
inserted = true;
}
[Link]([Link]());
currentIndex++;
}
if (index >= currentIndex) {
[Link](value);
inserted = true;
}
if (!inserted) {
[Link](value);
}
while (![Link]()) {
[Link]([Link]());
}
}
// sets the value at a specific index of a queue, x = {1, 2}; setQ(x, 0,
4); x = {4, 2}
public static <T> void setQ(Queue<T> queue, int index, T value) {
Queue<T> tempQueue = new Queue<>();
int i = 0;
while (i <= index && ![Link]()) {
T current = [Link]();
if (i == index) {
current = value;
}
[Link](current);
i++;
}
while (![Link]()) {
[Link]([Link]());
}
while (![Link]()) {
[Link]([Link]());
}
}
// gets the value at a specific index of a queue, x = {1, 2}; int y =
getQ(x, 0); y = 1
public static <T> T getQ(Queue<T> queue, int index) {
int size = 0;
Queue<T> tempQueue = new Queue<>();
T result = null;
while (![Link]()) {
T current = [Link]();
[Link](current);
size++;
if (size == index + 1) {
result = current;
}
}
while (![Link]()) {
[Link]([Link]());
}
return result;
}
// creates a copy of another Queue, x = {1, 2, 3, 4} Queue<Integer> y
= copyQ(x) = x
public static <T> Queue<T> copyQ(Queue<T> originalQueue) {
Queue<T> copy = new Queue<>();
Queue<T> tempQueue = new Queue<>();
while (![Link]()) {
T element = [Link]();
[Link](element);
[Link](element);
}
while (![Link]()) {
[Link]([Link]());
}
return copy;
}
// deletes a specific index of a Queue, x = {1, 2, 3, 4}; deleteQ(x, 0);
results in x = {2, 3, 4}
public static <T> void deleteQ(Queue<T> queue, int index) {
Queue<T> tempQueue = new Queue<>();
int currentIndex = 0;
while (![Link]()) {
if (currentIndex != index) {
[Link]([Link]());
} else {
[Link]();
}
currentIndex++;
}
while (![Link]()) {
[Link]([Link]());
}
}
//gets the size of a Queue, x = {1, 2, 3, 4}; sizeQ(x) = 4
public static <T> int sizeQ(Queue<T> x) {
Queue<T> tempQueue = new Queue<T>();
Queue<T> tempQueue2 = new Queue<T>();
while(![Link]()){
[Link]([Link]());
[Link]([Link]());
}
while (![Link]()){
[Link]([Link]());
}
int size = 0;
while (![Link]()) {
[Link]();
size++;
}
return size;
}
//checks if a Queue contains a specific value
public static <T> boolean containsValQ(Queue<T> queue, T value) {
Queue<T> tempQ = copyQ(queue);
while (![Link]()) {
if ([Link]().equals(value)) {
return true;
}
}
return false;
}
// reverses a Queue
public static <T> void reverseQ(Queue<T> queue) {
if ([Link]() || queue == null) return;
T x = [Link]();
reverseQ(queue);
[Link](x);
}
// checks if a Queue is a sub queue of another queue, x = {1, 2, 3, 4};
y = {2, 3};
subQ(x, y) = true, subQ(y, x) = false, whole of x isn’t a part of y
public static <T> boolean subQ(Queue<T> x, Queue<T> y) {
if ([Link]())return true;
if ([Link]() || sizeQ(x) < sizeQ(y)) return false;
Queue<T> tempX = copyQueueQ(x);
Queue<T> tempY = copyQueueQ(y);
while (![Link]()) {
T elementX = [Link]();
T elementY = [Link]();
if ([Link](elementY)) {
[Link]();
if ([Link]()) {
return true;
}
} else {
tempY = copyQueueQ(y);
}
}
return false;
}
// counts how many times there is a chosen value in a Queue, x = {1,
2, 1}
int value = countOccQ(x, 1) = 2
public static <T> int countOccQ(Queue<T> queue, T value) {
int count = 0;
Queue<T> tempQueue = new Queue<>();
while (![Link]()) {
T current = [Link]();
if ([Link](value)) {
count++;
}
[Link](current);
}
while(![Link]()) [Link]([Link]());
return count;
}
//creates a reversed version of a Queue, x = {1, 2, 3, 4};
Queue<Integer> y = reverseA(x) = {4, 3, 2, 1}
public static <T> Queue<T> reverseA(Queue<T> queue) {
Queue<T> temp = copyQ(queue);
Queue<T> reversedQueue = new Queue<>();
reverseQB(temp, reversedQueue);
return reversedQueue;
}
public static <T> void reverseQB(Queue<T> queue, Queue<T>
reversedQueue) {
if ([Link]()) {
return;
}
T element = [Link]();
reverseQB(queue, reversedQueue);
[Link](element);
}
//checks if a Queue is a palindrome a word, phrase, or sequence that
reads the same backward as forward
public static <T> boolean isPalindrome(Queue<T> queue) {
if ([Link]() || sizeQ(queue) == 1) {
return true;
}
Queue<T> reversedQueue = reverseA(queue);
while (![Link]()) {
T frontElement = [Link]();
T reversedElement = [Link]();
if () {
return false;
}
}
return true;
}
//removes only the none first consecutive occurrences of every value in
a Queue
After using the method on x = {1, 1, 2, 3, 2, 2, 1}, x will be {1, 2, 3, 2,
1}
public static <T> void removeConsecutiveOccurrencesQ(Queue<T>
queue) {
if ([Link]() || sizeQ(queue) == 1) return;
Queue<T> tempQueue = new Queue<>();
T prev = [Link]();
[Link](prev);
while (![Link]()) {
T current = [Link]();
if () {
[Link](current);
prev = current;
}
}
while (![Link]()) {
[Link]([Link]());
}
}
// deletes every index of a Node at which there is a specific value at.
public static <T> void deleteQT(Queue<T> head, T value) {
for(int i = 0; i<sizeQ(head); i++){
if(getQ(head, i) == value) {
deleteQ(head, i);
i--;
}
// splits a Queue into a Queue of Queues with the sizeQ of n,
Queue<Integer> x = {1, 2, 3, 4, 5}, Queue<Queue<Integer>> y =
split(x, 4)
results in y = {{1, 2}, {3}, {4}, {5}}
Another example: Queue<Integer> x = {1, 2, 3, 4, 5};
Queue<Queue<Integer>> y = split(x, 2); results in y = {{1, 2, 3}, {4,
5}}
public static <T> Queue<Queue<T>> splitQ(Queue<T> q, int n) {
if ([Link]() || n <= 0) return null;
int size = sizeQ(q);
int partSize = size / n;
int remainder = size % n;
Queue<Queue<T>> resultQueue = new Queue<>();
Queue<T> currentPart = null;
for (int i = 0; i < n; i++) {
currentPart = new Queue<>();
int currentPartSize = partSize + (i < remainder ? 1 : 0);
for (int j = 0; j < currentPartSize; j++) {
if (![Link]()) {
[Link]([Link]());
}
}
[Link](currentPart);
}
return resultQueue;
}
//creates a Queue which is the sorted version of queue
public static Queue<Integer> sortQ(Queue<Integer> queue) {
Queue<Integer> sortedQueue = new Queue<>();
while (![Link]()) {
int current = [Link]();
Queue<Integer> tempQueue = new Queue<>();
boolean inserted = false;
while (![Link]()) {
int value = [Link]();
if (!inserted && current < value) {
[Link](current);
inserted = true;
}
[Link](value);
}
if (!inserted) {
[Link](current);
}
while (![Link]()) {
[Link]([Link]());
}
}
return sortedQueue;
}
//The method takes the selected Queue(queue) and will create a new
splited Queue out of it according the the chosen start and end. for
example:
A = {1, 2, 3, 4, 5, 6} and B = CSplitQ(A, 1, 3) so it'll result in B = {{1},
{2, 3, 4}, {5 ,6}} because {2, 3, 4} are at indexes 1-3, 2 is at index 1
and 4 is at index 3
B is a Queue<Queue<Integer>> in this example.
public static <T> Queue<Queue<T>> CSplitQ(Queue<T> queue, int
start, int end) {
if ([Link]() || start > end) return null;
Queue<Queue<T>> resultQueue = new Queue<>();
Queue<T> firstPart = new Queue<>();
Queue<T> middlePart = new Queue<>();
Queue<T> lastPart = new Queue<>();
int index = 0;
while (![Link]() && index < start) {
[Link]([Link]());
index++;
}
while (![Link]() && index <= end) {
[Link]([Link]());
index++;
}
while (![Link]()) [Link]([Link]());
if (![Link]()) [Link](firstPart);
if (![Link]()) [Link](middlePart);
if (![Link]()) [Link](lastPart);
return resultQueue;
}
//find the min value in the queue
public static int findMinQ(Queue<Integer> queue) {
if ([Link]())return 0; // a mistake, it should return nothing
Queue<Integer> tempQueue = new Queue<>();
int min = Integer.MAX_VALUE;
while (![Link]()) {
int current = [Link]();
if (current < min) {
min = current;
}
[Link](current);
}
while (![Link]())
[Link]([Link]());
return min;
}
//finds the max value in the queue
public static <T> int findMaxQ(Queue<Integer> queue) {
if ([Link]()) return 0; // a mistake it should return nothing
Queue<Integer> tempQueue = new Queue<>();
int max = Integer.MIN_VALUE;
while (![Link]()) {
int current = [Link]();
if (current > max) {
max = current;
}
[Link](current);
}
while (![Link]()) [Link]([Link]());
return max;
}
Magnitude of running time, Order of DE via degree of n
1. moveTo = O(n)
2. OcValQ = O(n)
3. removeNoneFirstOccurrencesQ = O(n)
4. insertQ = O(n)
5. setQ = O(n)
6. getQ = O(n)
7. copyQ = O(n)
8. delete = O(n)
9. sizeQ = O(n)
10. containsValueQ = O(n)
11. reverseQ = O(n)
12. subQ = O(n)
13. countOccQ = O(n)
14. reverseA = O(n)
15. isPalindrome = O(n)
16. removeConsecutiveOccurrences = O(n)
17. deleteQT = O(n^2)
18. splitQ = O(n)
19. sortQ = O(n)
20. CSplitQ = O(n)
21. findMinQ = O(n)
22. findMaxQ = O(n)
//How to create a Queue:
Queue<data type> x = new Queue<>();
How to add values to the Queue:
(Name of Queue).insert(value of same data type of the Queue)
Queue<Integer> x = new Queue<>();
[Link](1) – index 0
[Link](2) – index 1
// The Queue class:
public boolean isEmpty() {
return [Link] == null;
}
public void insert(T x) {
if ([Link] == null) {
[Link] = new Node(x);
[Link] = [Link];
} else {
[Link](new Node(x));
[Link] = [Link]();
}
}
public T remove() {
T x = [Link]();
Node<T> pos = [Link];
[Link] = [Link]();
if ([Link] == null) {
[Link] = null;
}
[Link]((Node)null);
return x;
}
public T head() {
return [Link]();
}
public String toString() {
Node<T> pos = [Link];
String str;
for(str = "["; pos != null; pos = [Link]()) {
str = str + [Link]().toString();
if ([Link]() != null) {
str = str + ",";
}
}
str = str + "]";
return str;
}