master > mater: code - nextGreaterElement, auxiliary methods entfernt

This commit is contained in:
RLogik
2021-11-07 18:56:49 +01:00
parent 3505401c7f
commit 6541a5246d
2 changed files with 9 additions and 48 deletions

View File

@@ -14,7 +14,7 @@ import (
* GLOBAL VARIABLES/CONSTANTS
* ---------------------------------------------------------------- */
var _output_list [][2]int
//
/* ---------------------------------------------------------------- *
* ALGORITHM next greater element
@@ -26,7 +26,7 @@ Inputs: L = Liste von Zahlen, x = Zahl.
Outputs: Liste von Paaren von Elementen und ihrem nächsten größeren Element
*/
func NextGreaterElement(L []int) [][2]int {
clearOutput()
output := [][2]int{}
S := stacks.CREATE()
S.INIT()
@@ -43,7 +43,7 @@ func NextGreaterElement(L []int) [][2]int {
if element < nextElement {
// falls top Element < next Element, zum Output hinzufügen und vom Stack entfernen
logging.Debug("Stack S | %v; top Element > nextElement; ==> pop und Paar zum Output hinzufügen", S)
addToOutput(element, nextElement)
output = append(output, [2]int{element, nextElement})
S.POP()
metrics.AddMovesCost()
logging.Debug("Stack S | %v", S)
@@ -70,31 +70,12 @@ func NextGreaterElement(L []int) [][2]int {
for !S.EMPTY() { // ACHTUNG: schreibe 'while' im Pseudocode, denn dies ist eine while-Schleife in golang
logging.Debug("Stack S | %v", S)
element := S.TOP()
output = append(output, [2]int{element, -1})
S.POP()
metrics.AddMovesCost()
addToOutput(element, -1)
}
logging.Debug("Stack S | %v", S)
return output()
}
/* ---------------------------------------------------------------- *
* AUXILIARY METHODS
* ---------------------------------------------------------------- */
func clearOutput() {
_output_list = [][2]int{}
}
func addToOutput(m int, n int) {
_output_list = append(_output_list, [2]int{m, n})
}
func output() [][2]int {
var output = make([][2]int, len(_output_list))
copy(output, _output_list)
clearOutput()
return output
}