Creating a generic queue

Report a typo

A queue is a linear collection of objects that are inserted and removed according to the first-in, first-out (FIFO) principle. In short, a queue is an ordered list where insertions occur at the back and deletions occur at the front

A common example of a queue is a line of people waiting to buy tickets at a movie theater. Any new people that join the line are added at the end of the line, while the first person at the front of the queue gets to buy their ticket and then leaves the queue.

a queue

Now that you know what a queue is, your task is to create two generic methods: enqueue and dequeue. The enqueue method must add one element to the back of the queue, while dequeue must remove one element at the front.

We have already created the generic Queue type for you, it is a slice that can store any type of elements. Now, it's your turn to write additional required code for the enqueue and dequeue methods.

Note that for this task, you must not write or modify any code within the main() function. Your only task is to write the additional code to finish the implementation of the enqueue and dequeue methods.

Sample Input 1:

Person1 Person2 Person3
1 2 3

Sample Output 1:

[Person3]
[2 3]
Write a program in Go
package main

import "fmt"

type Queue[T any] []T // DO NOT modify the declaration of the `Queue` type!

// Write the additional required code to finish the
// implementation of the `enqueue` and `dequeue` methods below:

func (q *Queue[T]) enqueue(v ?) {
*q = append(?, ?)
}

func (q *Queue[T]) dequeue() ? {
if len(?) == 0 {
var zero ?
return zero
}
v := (*q)[?]
*q = (*q)[?:]

return v
}

// DO NOT delete or modify the contents of the main function!
func main() {
var que1 Queue[any]
var que2 Queue[any]

var str1, str2, str3 string
fmt.Scanln(&str1, &str2, &str3)

que1.enqueue(str1)
que1.enqueue(str2)
que1.enqueue(str3)
que1.dequeue()
___

Create a free account to access the full topic