Overview
Referencing the Algorithm Encyclopedia, we learn about algorithms and data structures.
The implementation is also available on github - bmf-san/road-to-algorithm-master.
Queue
- A structure where data is lined up in a row so that only the data added first can be accessed.
- The direction of addition and removal is opposite to that of a stack.
- FIFO (First In First Out)
- First come, first served
- Also known as a waiting line.
- Adding data is called enqueue, and removing data is called dequeue.
Computational Time
Depends on the implementation form such as arrays or linked lists.
Implementation
package main
// Queue is a queue.
type Queue struct {
nodes []*Node
}
// Node is a item of a stack.
type Node struct {
value string
}
// newQueue create a Stack.
func newQueue() *Queue {
return &Queue{}
}
// enqueue adds an node to the end of the queue.
func (s *Queue) enqueue(n *Node) {
s.nodes = append(s.nodes, n)
}
// dequeue removes an node from the top of the queue.
func (s *Queue) dequeue() {
s.nodes = s.nodes[1:len(s.nodes)]
}
- The implementation is almost the same as a stack. Only the slice index access is different.
- Note
