master > master: code - stacks + queues data structures überarbeitet
This commit is contained in:
parent
5c13f8d4cd
commit
8d2de4fa2b
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:]
|
||||
}
|
@ -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)]
|
||||
}
|
||||
|
52
code/python/src/data_structures/queues.py
Normal file
52
code/python/src/data_structures/queues.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from local.typing import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBAL VARIABLES/CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CLASS Queue
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Queue(object):
|
||||
_initialised: bool;
|
||||
values: List[int];
|
||||
|
||||
def __init__(self):
|
||||
self._initialised = False;
|
||||
|
||||
def INIT(self):
|
||||
self.values = [];
|
||||
self._initialised = True;
|
||||
|
||||
def EMPTY(self) -> bool:
|
||||
return not self._initialised or len(self.values) == 0;
|
||||
|
||||
def ENQUEUE(self, x: int):
|
||||
if not self._initialised:
|
||||
raise Exception('Queue not initialised!')
|
||||
self.values.append(x);
|
||||
|
||||
def FRONT(self) -> int:
|
||||
if self.EMPTY():
|
||||
raise Exception('Cannot read from empty queue!')
|
||||
return self.values[0];
|
||||
|
||||
def DEQUEUE(self) -> int:
|
||||
if self.EMPTY():
|
||||
raise Exception('Cannot remove from empty queue!')
|
||||
self.values = self.values[1:];
|
||||
|
||||
def __str__(self) -> str:
|
||||
if len(self.values) == 0:
|
||||
return '-';
|
||||
return ', '.join([str(x) for x in self.values]);
|
@ -18,26 +18,33 @@ from local.typing import *;
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class Stack(object):
|
||||
_initialised: bool;
|
||||
values: List[int];
|
||||
|
||||
def __init__(self):
|
||||
self._initialised = False;
|
||||
|
||||
def INIT(self):
|
||||
self.values = [];
|
||||
self._initialised = True;
|
||||
|
||||
def EMPTY(self) -> bool:
|
||||
return len(self.values) == 0;
|
||||
return not self._initialised or len(self.values) == 0;
|
||||
|
||||
def PUSH(self, x: int):
|
||||
if not self._initialised:
|
||||
raise Exception('Stack not initialised!');
|
||||
self.values.append(x);
|
||||
|
||||
def TOP(self) -> int:
|
||||
if self.EMPTY():
|
||||
raise Exception('Cannot pop from empty stack!')
|
||||
raise Exception('Cannot read from empty stack!');
|
||||
return self.values[-1];
|
||||
|
||||
def POP(self) -> int:
|
||||
x = self.TOP()
|
||||
self.values = self.values[:-1]
|
||||
return x
|
||||
if self.EMPTY():
|
||||
raise Exception('Cannot remove from empty stack!');
|
||||
self.values = self.values[:-1];
|
||||
|
||||
def __str__(self) -> str:
|
||||
if len(self.values) == 0:
|
||||
|
Loading…
Reference in New Issue
Block a user