master > master: code - stacks + queues data structures überarbeitet

This commit is contained in:
RLogik
2021-11-07 08:49:52 +01:00
parent 5c13f8d4cd
commit 8d2de4fa2b
4 changed files with 186 additions and 37 deletions

View File

@@ -20,7 +20,7 @@ import (
* ---------------------------------------------------------------- */
type StackInt struct {
values []int
values *[]int
}
/* ---------------------------------------------------------------- *
@@ -28,45 +28,54 @@ type StackInt struct {
* ---------------------------------------------------------------- */
func CREATE() StackInt {
return StackInt{
values: []int{},
return StackInt{}
}
func (S *StackInt) INIT() {
S.values = &[]int{}
}
func (S StackInt) Length() int {
if S.values == nil {
return 0
}
}
func (S *StackInt) INIT() StackInt {
return StackInt{
values: []int{},
}
}
func (S StackInt) EMPTY() bool {
return len(S.values) == 0
}
func (S *StackInt) PUSH(x int) {
S.values = append(S.values, x)
}
func (S *StackInt) TOP() int {
if S.EMPTY() {
panic("Cannot pop from empty stack!")
}
return S.values[len(S.values)-1]
}
func (S *StackInt) POP() int {
x := S.TOP()
S.values = S.values[:(len(S.values) - 1)]
return x
return len(*S.values)
}
func (S StackInt) String() string {
if len(S.values) == 0 {
if S.EMPTY() {
return "-"
}
values := []string{}
for _, value := range S.values {
for _, value := range *S.values {
values = append(values, fmt.Sprintf("%v", value))
}
return fmt.Sprintf(strings.Join(values, ", "))
}
func (S StackInt) EMPTY() bool {
return S.Length() == 0
}
func (S *StackInt) PUSH(x int) {
if S.values == nil {
panic("Stack not initialised!")
}
*S.values = append(*S.values, x)
}
func (S *StackInt) TOP() int {
if S.EMPTY() {
panic("Cannot read from empty stack!")
}
n := S.Length()
return (*S.values)[n-1]
}
func (S *StackInt) POP() {
if S.EMPTY() {
panic("Cannot remove from empty stack!")
}
n := S.Length()
*S.values = (*S.values)[:(n - 1)]
}