A detailed explanation of the implementation of data structure queue in golang



This article mainly introduces the related content about the implementation of data structure queue in golang, and shares it for your reference and learning. I won’t say much next, let’s take a look at the detailed introduction.


The basic operations of queue are initialization, getting size, adding elements and removing elements. The most important feature is to meet the first in, first out.


Next, we will follow the previous routine step by step to analyze how to use the syntax features of go to implement the queue data structure.


First, define the node structure of each node. As usual, the value type can be any type. The pointer type of the front and back pointer fields of the node is node

type node struct {
 value interface{}
 prev *node
 next *node

Continue to define the linked list structure, define the pointers of the head node and the tail node, and define the queue size:

type LinkedQueue struct {
 head *node
 tail *node
 size int


To get the queue size, just get the size in linkedqueue:

func (queue *LinkedQueue) Size() int {
 return queue.size


The peek operation only needs to get the element of the queue header, without deletion. The return type is any type, which can be implemented by the interface. In addition, if the head pointer field is nil, you need to throw an exception with panic. If everything is OK, return the value of the team head node:

func (queue *LinkedQueue) Peek() interface{} {
 if queue.head == nil {
 panic("Empty queue.")
 return queue.head.value

Add to

Adding operation is an important operation in the queue. It also needs to distinguish whether the end node of the queue is nil. According to whether it is nil, different connection operations are performed. The size of the last queue needs to be increased by 1. In order not to waste memory, the pointer variable of the new node needs to be set nil:

func (queue *LinkedQueue) Add(value interface{}) {
 new_node := &node{value, queue.tail, nil}
 if queue.tail == nil {
 queue.head = new_node
 queue.tail = new_node
 } else {
 queue.tail.next = new_node
 queue.tail = new_node
 new_node = nil


The delete operation of the queue is also very simple, nothing more than the disconnection operation of the node. Before that, we need to determine whether the state of the linked list is nil? Then the node at the front end of the removed queue uses a new variable node to save the node at the front of the queue. After a series of operations, it goes to nil and reduces the length.

func (queue *LinkedQueue) Remove() {
 if queue.head == nil {
 panic("Empty queue.")
 first_node := queue.head
 queue.head = first_node.next
 first_node.next = nil
 first_node.value = nil
 first_node = nil

OK, the above is the process of implementing queue with the basic syntax features of go. Thank you for reading!!!


The above is the whole content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message and communicate with us. Thank you for your support for developpaer.

Recommended Today

Analysis of new data type instances of ES6 learning notes

An example of this paper describes the new data types of ES6 learning notes. To share with you for your reference, as follows: 1. Data deconstruction assignment 1. Deconstruction and assignment of arrays Basic usage: let [key1, key2…] = [value1, Value2…] Let [name, age, sex] = [‘xiaoming ‘, 24,’ male ‘]; console.log(name); console.log(age); console.log(sex); Note […]