0% found this document useful (0 votes)
3 views

Queue Methods

The document describes various methods for manipulating a queue data structure, including moving elements, counting occurrences, and modifying values at specific indexes. It also covers functionalities like removing duplicates, reversing the queue, and splitting it into sub-queues. Additionally, it includes methods for checking if a queue is a palindrome and finding the minimum value within the queue.

Uploaded by

lollmaolmaolol04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Queue Methods

The document describes various methods for manipulating a queue data structure, including moving elements, counting occurrences, and modifying values at specific indexes. It also covers functionalities like removing duplicates, reversing the queue, and splitting it into sub-queues. Additionally, it includes methods for checking if a queue is a palindrome and finding the minimum value within the queue.

Uploaded by

lollmaolmaolol04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

// 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++) {


tempQueue.insert(q.remove());
}
for (int i = start; i <= end; i++) {
movedElements.insert(q.remove());
}
while (!q.isEmpty()) {
tempQueue.insert(q.remove());
}
while (!movedElements.isEmpty()) {
tempQueue.insert(movedElements.remove());
}

while (!tempQueue.isEmpty()) {
q.insert(tempQueue.remove());
}
}else {
Queue<T> temp = new Queue<>();
Queue<T> moved = new Queue<>();

int i = 0;
while (i < start) {
temp.insert(q.remove());
i++;
}

i = start;
while (i <= end) {
moved.insert(q.remove());
i++;
}

while (!q.isEmpty()) {
temp.insert(q.remove());
}

i = 0;
while (i < newStart) {
q.insert(temp.remove());
i++;
}

while (!moved.isEmpty()) {
q.insert(moved.remove());
}

while (!temp.isEmpty()) {
q.insert(temp.remove());
}
}
}
// 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 (!tempQueue.isEmpty()) {
T element = tempQueue.remove();
if (element.equals(val)) {
indices.insert(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 (queue.isEmpty()) return;

Queue<T> uniqueElements = new Queue<>();


Queue<T> tempQueue = new Queue<>();

while (!queue.isEmpty()) {
T current = queue.remove();
if (!containsElementQ(uniqueElements, current)) {
uniqueElements.insert(current);
tempQueue.insert(current);
}
}

while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
}
// 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 (!queue.isEmpty()) {
if (currentIndex == index) {
tempQueue.insert(value);
inserted = true;
}
tempQueue.insert(queue.remove());
currentIndex++;
}
if (index >= currentIndex) {
tempQueue.insert(value);
inserted = true;
}
if (!inserted) {
tempQueue.insert(value);
}
while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
}
// 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 && !queue.isEmpty()) {


T current = queue.remove();
if (i == index) {
current = value;
}
tempQueue.insert(current);
i++;
}
while (!queue.isEmpty()) {
tempQueue.insert(queue.remove());
}
while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
}
// 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 (!queue.isEmpty()) {
T current = queue.remove();
tempQueue.insert(current);
size++;
if (size == index + 1) {
result = current;
}
}
while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
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 (!originalQueue.isEmpty()) {
T element = originalQueue.remove();
copy.insert(element);
tempQueue.insert(element);
}
while (!tempQueue.isEmpty()) {
originalQueue.insert(tempQueue.remove());
}
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 (!queue.isEmpty()) {
if (currentIndex != index) {
tempQueue.insert(queue.remove());
} else {
queue.remove();
}
currentIndex++;
}

while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
}

//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(!x.isEmpty()){
tempQueue.insert(x.head());
tempQueue2.insert(x.remove());
}
while (!tempQueue2.isEmpty()){
x.insert(tempQueue2.remove());
}
int size = 0;

while (!tempQueue.isEmpty()) {
tempQueue.remove();
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 (!tempQ.isEmpty()) {
if (tempQ.remove().equals(value)) {
return true;
}
}
return false;
}

// reverses a Queue

public static <T> void reverseQ(Queue<T> queue) {


if (queue.isEmpty() || queue == null) return;
T x = queue.remove();
reverseQ(queue);
queue.insert(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 (y.isEmpty())return true;

if (x.isEmpty() || sizeQ(x) < sizeQ(y)) return false;

Queue<T> tempX = copyQueueQ(x);


Queue<T> tempY = copyQueueQ(y);

while (!tempX.isEmpty()) {
T elementX = tempX.remove();
T elementY = tempY.head();

if (elementX.equals(elementY)) {
tempY.remove();
if (tempY.isEmpty()) {
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 (!queue.isEmpty()) {
T current = queue.remove();
if (current.equals(value)) {
count++;
}
tempQueue.insert(current);
}
while(!tempQueue.isEmpty()) queue.insert(tempQueue.remove());
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 (queue.isEmpty()) {
return;
}

T element = queue.remove();
reverseQB(queue, reversedQueue);
reversedQueue.insert(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 (queue.isEmpty() || sizeQ(queue) == 1) {
return true;
}

Queue<T> reversedQueue = reverseA(queue);

while (!queue.isEmpty()) {
T frontElement = queue.remove();
T reversedElement = reversedQueue.remove();

if (!frontElement.equals(reversedElement)) {
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 (queue.isEmpty() || sizeQ(queue) == 1) return;

Queue<T> tempQueue = new Queue<>();


T prev = queue.remove();
tempQueue.insert(prev);

while (!queue.isEmpty()) {
T current = queue.remove();
if (!current.equals(prev)) {
tempQueue.insert(current);
prev = current;
}
}
while (!tempQueue.isEmpty()) {
queue.insert(tempQueue.remove());
}
}

// 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 (q.isEmpty() || 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 (!q.isEmpty()) {
currentPart.insert(q.remove());
}
}

resultQueue.insert(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 (!queue.isEmpty()) {
int current = queue.remove();
Queue<Integer> tempQueue = new Queue<>();
boolean inserted = false;

while (!sortedQueue.isEmpty()) {
int value = sortedQueue.remove();
if (!inserted && current < value) {
tempQueue.insert(current);
inserted = true;
}
tempQueue.insert(value);
}
if (!inserted) {
tempQueue.insert(current);
}
while (!tempQueue.isEmpty()) {
sortedQueue.insert(tempQueue.remove());
}
}
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 (queue.isEmpty() || 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 (!queue.isEmpty() && index < start) {
firstPart.insert(queue.remove());
index++;
}

while (!queue.isEmpty() && index <= end) {


middlePart.insert(queue.remove());
index++;
}

while (!queue.isEmpty()) lastPart.insert(queue.remove());

if (!firstPart.isEmpty()) resultQueue.insert(firstPart);
if (!middlePart.isEmpty()) resultQueue.insert(middlePart);
if (!lastPart.isEmpty()) resultQueue.insert(lastPart);

return resultQueue;
}
//find the min value in the queue

public static int findMinQ(Queue<Integer> queue) {


if (queue.isEmpty())return 0; // a mistake, it should return nothing

Queue<Integer> tempQueue = new Queue<>();


int min = Integer.MAX_VALUE;

while (!queue.isEmpty()) {
int current = queue.remove();
if (current < min) {
min = current;
}
tempQueue.insert(current);
}
while (!tempQueue.isEmpty())
queue.insert(tempQueue.remove());

return min;
}
//finds the max value in the queue
public static <T> int findMaxQ(Queue<Integer> queue) {
if (queue.isEmpty()) return 0; // a mistake it should return nothing

Queue<Integer> tempQueue = new Queue<>();


int max = Integer.MIN_VALUE;

while (!queue.isEmpty()) {
int current = queue.remove();
if (current > max) {
max = current;
}
tempQueue.insert(current);
}
while (!tempQueue.isEmpty()) queue.insert(tempQueue.remove());

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<>();

x.insert(1) – index 0

x.insert(2) – index 1
// The Queue class:

public boolean isEmpty() {


return this.first == null;
}
public void insert(T x) {
if (this.first == null) {
this.first = new Node(x);
this.lastPos = this.first;
} else {
this.lastPos.setNext(new Node(x));
this.lastPos = this.lastPos.getNext();
}
}
public T remove() {
T x = this.first.getValue();
Node<T> pos = this.first;
this.first = this.first.getNext();
if (this.first == null) {
this.lastPos = null;
}
pos.setNext((Node)null);
return x;
}
public T head() {
return this.first.getValue();
}
public String toString() {
Node<T> pos = this.first;

String str;
for(str = "["; pos != null; pos = pos.getNext()) {
str = str + pos.getValue().toString();
if (pos.getNext() != null) {
str = str + ",";
}
}
str = str + "]";
return str;
}

You might also like