Complete the code to add an element to the end of a queue.
queue := []int{}
queue = append(queue, [1])In Go, to add an element to the end of a slice (used as a queue), we use append with the element name. Here, element is the correct placeholder for the item to add.
Complete the code to remove the last element from a stack.
stack = stack[:len(stack) [1] 1]
To remove the last element from a stack implemented as a slice, we slice up to len(stack)-1. The minus sign - is used to subtract 1.
Fix the error in the code to check if a queue is empty.
if len(queue) [1] 0 { fmt.Println("Queue is empty") }
To check if a queue is empty, we compare its length to zero using ==. If length equals zero, the queue is empty.
Fill both blanks to implement pushing an element onto a stack and then popping it.
stack = append(stack, [1]) last := stack[len(stack) [2] 1]
To push, append the element to the stack. To pop, access the last element using len(stack)-1.
Fill all three blanks to create a queue, enqueue an element, and dequeue it.
queue := []int{}
queue = append(queue, [1])
dequeued := queue[[2]]
queue = queue[[3]:]We start with an empty queue. Enqueue by appending element. Dequeue by taking the first element at index 0 and then slicing the queue from index 1 onward.
