Have you ever stood in a queue, The queue data structure also does the same thing. You stand in the end of line when you want to place an order maybe in your favorite self service restaurant and then you move up the queue and leave.
The Queue data structure in CS performs the same functionality. The Queue Data structure is a First In First Out data structure. The Queue Data structure can be built with two basic functions Enqueue and Dequeue which basically is adding to the list and removing from the list.
In the real world of computer science Queues form the backbone of the following software components
While a simple easy to visualize component would be
package main import ( "fmt" "errors" ) type Queue struct{ Elements []int Size int } func (q *Queue) Enqueue(value int){ if q.GetLength() == q.Size { fmt.Println("Overflow") return } q.Elements = append(q.Elements, value) } func (q *Queue) Dequeue() int { if q.IsEmpty() { fmt.Println("UnderFlow") return 0 } element := q.Elements[0] if q.GetLength() == 1 { q.Elements = nil return element } q.Elements = q.Elements[1:] return element } func (q *Queue) GetLength() int { return len(q.Elements) } func (q *Queue) IsEmpty() bool { return len(q.Elements) == 0 } func (q *Queue) Peek() (int, error) { if q.IsEmpty() { return 0, errors.New("empty queue") } return q.Elements[0], nil }
Time Complexity - O(1) for Enqueue and Dequeue
Space complexity - O(1) for Enqueue and Dequeue
Reference - https://www.geeksforgeeks.org/queue-in-go-language/
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3