master > master: code - stacks + queues data structures überarbeitet
This commit is contained in:
81
code/golang/pkg/data_structures/queues/queues.go
Normal file
81
code/golang/pkg/data_structures/queues/queues.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package queues
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES/CONSTANTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* TYPE
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
type QueueInt struct {
|
||||
values *[]int
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS stacks
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func CREATE() QueueInt {
|
||||
return QueueInt{}
|
||||
}
|
||||
|
||||
func (S *QueueInt) INIT() {
|
||||
S.values = &[]int{}
|
||||
}
|
||||
|
||||
func (S QueueInt) Length() int {
|
||||
if S.values == nil {
|
||||
return 0
|
||||
}
|
||||
return len(*S.values)
|
||||
}
|
||||
|
||||
func (S QueueInt) String() string {
|
||||
if S.EMPTY() {
|
||||
return "-"
|
||||
}
|
||||
values := []string{}
|
||||
n := S.Length()
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
value := (*S.values)[i]
|
||||
values = append(values, fmt.Sprintf("%v", value))
|
||||
}
|
||||
return fmt.Sprintf(strings.Join(values, " -> "))
|
||||
}
|
||||
|
||||
func (S QueueInt) EMPTY() bool {
|
||||
return S.Length() == 0
|
||||
}
|
||||
|
||||
func (S *QueueInt) ENQUEUE(x int) {
|
||||
if S.values == nil {
|
||||
panic("Queue not initialised!")
|
||||
}
|
||||
*S.values = append(*S.values, x)
|
||||
}
|
||||
|
||||
func (S *QueueInt) FRONT() int {
|
||||
if S.EMPTY() {
|
||||
panic("Cannot read from empty queue!")
|
||||
}
|
||||
return (*S.values)[0]
|
||||
}
|
||||
|
||||
func (S *QueueInt) DEQUEUE() {
|
||||
if S.EMPTY() {
|
||||
panic("Cannot remove from empty queue!")
|
||||
}
|
||||
*S.values = (*S.values)[1:]
|
||||
}
|
||||
Reference in New Issue
Block a user