In: Computer Science
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.
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:)