Question

In: Computer Science

Write out a algorithm that sets last equal to the last element in a queue, leaving...

Write out a algorithm that sets last equal to the last element in a queue, leaving the queue unchanged. Write out a algorithm to create a copy of myQueue, leaving myQueue unchanged. Write out a algorithm Replace that takes a stack and two items. If the first item is in the stack, replace it with the second item, leaving the rest of the stack unchanged. Write out a algorithm Replace that takes a queue and two items. If the first item is in the queue, replace it with the second item, leaving the rest of the queue unchanged.  

Solutions

Expert Solution

ANSWER :

Find the code for the above question below, read the comments provides in the code for better understanding.

Question 1

1.Write an algorithm that sets last equal to the last element in a queue, leaving the queue unchanged.


Answer :

We need to keep the queue same as original so we will do is use a temporary queue and dequeue everything from the given queue and enqueue it to new temporary queue. While doing so keep storing the elements to last , after it’s done finally enqueue everything from temporary queue to original queue.

While(not isEmpty(originalQueue))
Dequeeu(originalQueue, last)
Enqueue(tempQueue,last)

While(Not isEmpty(tempQueue))
Dequeue(tempQueue,item)
Enqueue(originalQueue,item)

Question 2

2. Write an algorithm to create a copy of myQueue, leaving myQueue unchanged.

Answer :

Again we will use a temporary queue and enqueue everything to it by dequeuing form the original queue. At the end , we will dequeue from tempqueue and enqueue into the original queue as well as the copyQueue.

While(not isEMpty(originalQueue))
Dequeue(originalQueue, last)
Enqueue(tempQueue,last)

While(Not isEmpty(tempQueue))
Dequeue(tempQueue,item)
Enqueue(originalQueue,item)
Enqueue(copyQueue,item)

Question 3

3. Write an algorithm Replace that takes a stack and two items. If the first item is in the stack, replace it with the second item, leaving the rest of the stack unchanged.

Answer :

To achieve this use a temporary stack and pop everything from it and push into new stack until we encounter the first item, if we find the first item , push second item into the temporary stack. Now we need to push everything back to the original stack , so again pop from temp stack and push into original stack.

Pop(originalStack, item)
While(item Not Equal firstItem)
push(tempStack, item)
pop(originalStack, item)
If(Not isEmpty(originalStack))
push(tempStack, secondItem)
While(Not isEmpty(tempStack))
Pop(tempStack, item)
Push(originalStack, item)

Question 4

4. Write an algorithm Replace that takes a queue and two items. If the first item is in the queue, replace it with the second item, leaving the rest of the queue unchanged.

Answer :

To achieve this use a temporary queue and dequeue everything from it and enqueue into new queue until we encounter the first item, if we find the first item , enqueue second item into the temporary queue . Now we need to dequeue everything back to the original queue , so again dequeue from temp queue and enqueue into original queue.

Dequeue(originalQueue, item)
While(item Not Equal firstItem)
Enqueue(tempQueue, item)
Dequeue(originalQueue, item)
If(Not isEmpty(originalQueue))
Enqueue (tempQueue, secondItem)
While(Not isEmpty(tempQueue))
Dequeue (tempQueue, item)
Enqueue (originalQueue, item)

Hope it helps... please give an upvote. it's very important to me... thank you:)


Related Solutions

Write algorithm for LIFO (Last In First Out) Page Replacement Algorithm
Write algorithm for LIFO (Last In First Out) Page Replacement Algorithm
Given a queue of integers, write an algorithm in Pseudocode that, using only the queue ADT,...
Given a queue of integers, write an algorithm in Pseudocode that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
[C++] Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that...
[C++] Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first.
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first, and...
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
In Haskell Write a function equal that returns whether two sets are equal. equal :: Set...
In Haskell Write a function equal that returns whether two sets are equal. equal :: Set -> Set -> Bool
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a statement to output on the console the valueof the last element in a...
Write a statement to output on the console the value of the last element in a single-dimensional array of integers called grades.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT