forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queuelinklistwithlist.go
70 lines (56 loc) · 1.72 KB
/
queuelinklistwithlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Queue Linked-List with standard library (Container/List)
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
// The order is First In First Out (FIFO).
// details:
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see queuearray.go, queuelinkedlist.go, queue_test.go
package queue
// container/list: is used as linked-list
// fmt: used to return fmt.Errorf for the error part
import (
"container/list"
"fmt"
)
// LQueue will be store the value into the list
type LQueue struct {
queue *list.List
}
// Enqueue will be added new value
func (lq *LQueue) Enqueue(value any) {
lq.queue.PushBack(value)
}
// Dequeue will be removed the first value that input (First In First Out - FIFO)
func (lq *LQueue) Dequeue() error {
if !lq.Empty() {
element := lq.queue.Front()
lq.queue.Remove(element)
return nil
}
return fmt.Errorf("dequeue is empty we got an error")
}
// Front it will return the front value
func (lq *LQueue) Front() (any, error) {
if !lq.Empty() {
val := lq.queue.Front().Value
return val, nil
}
return "", fmt.Errorf("error queue is empty")
}
// Back it will return the back value
func (lq *LQueue) Back() (any, error) {
if !lq.Empty() {
val := lq.queue.Back().Value
return val, nil
}
return "", fmt.Errorf("error queue is empty")
}
// Len it will return the length of list
func (lq *LQueue) Len() int {
return lq.queue.Len()
}
// Empty is check our list is empty or not
func (lq *LQueue) Empty() bool {
return lq.queue.Len() == 0
}