master > master: code - stacks + queues data structures überarbeitet
This commit is contained in:
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:
|
||||
|
||||
Reference in New Issue
Block a user