-
-
Save syranez/603529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cassert> | |
class ListenElement { | |
public: | |
ListenElement ( const int pValue, ListenElement * pNext = 0 ) { | |
value = pValue; | |
next = pNext; | |
} | |
ListenElement * getNext () { | |
return next; | |
} | |
void setNext ( ListenElement * pNext ) { | |
next = pNext; | |
} | |
int getValue () { | |
return value; | |
} | |
void setValue ( int pValue ) { | |
value = pValue; | |
} | |
private: | |
int value; | |
ListenElement *next; | |
}; | |
class Liste { | |
public: | |
Liste () { | |
head = 0; | |
size = 0; | |
} | |
void add ( const int value, size_t position = 0 ) { | |
if ( position > size ) { | |
throw std::string("Bad miss, boy."); | |
} | |
ListenElement * newElem = new ListenElement(value); | |
if ( newElem == 0 ) { | |
throw std::string("Got no memory. sigh."); | |
} | |
if ( position == 0 ) { | |
newElem->setNext(head); | |
head = newElem; | |
} else { | |
ListenElement * current = head; | |
position--; | |
while ( position != 0 ) { | |
current = current->getNext(); | |
position--; | |
} | |
newElem->setNext(current->getNext()); | |
current->setNext(newElem); | |
} | |
incSize(); | |
} | |
void remove ( size_t index ) { | |
if ( size == 0 || | |
index > (size-1) ) { | |
throw std::string("Bad miss, boy."); | |
} | |
ListenElement * current = head; | |
if ( index == 0 ) { | |
head = head->getNext(); | |
delete current; | |
decSize(); | |
} else { | |
while ( index != 1 ) { | |
current = current->getNext(); | |
index--; | |
} | |
ListenElement * delere = current->getNext(); | |
current->setNext(current->getNext()->getNext()); | |
delete delere; | |
decSize(); | |
} | |
} | |
int getValue ( size_t index ) { | |
if ( index > getSize() ) { | |
throw std::string("Bad miss, boy."); | |
} | |
ListenElement * current = head; | |
while ( index != 1 ) { | |
current = current ? current->getNext() : throw std::string("Internal error. add is shit. sry."); | |
index--; | |
} | |
return current->getValue(); | |
} | |
inline size_t getSize() { | |
return size; | |
} | |
private: | |
ListenElement * head; | |
size_t size; | |
inline void decSize() { | |
size = size != 0 ? size-- : 0; | |
} | |
inline void incSize() { | |
size++; | |
} | |
}; | |
int main ( int argc, char ** argv ) { | |
Liste foo; | |
assert(foo.getSize() == 0); | |
for ( size_t i = 1; i <= 10; i++ ) { | |
foo.add(i*10, i-1); | |
assert(foo.getSize() == i); | |
assert(foo.getValue(i) == i*10); | |
} | |
try { | |
foo.add(200, 234); | |
} catch ( std::string e ) { | |
std::cout << "Exception occured: " << e << "\n"; | |
} | |
try { | |
foo.remove(42); | |
} catch ( std::string e ) { | |
std::cout << "Exception occured: " << e << "\n"; | |
} | |
return 0; | |
} |
Das Exceptionhandling ist väry väry pöhse. (std::string kann im Konstruktor wiederum eine Exception werfen (std::terminate), die dann die aktuelle Exception überdeckt. -> schlecht).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mir ist langweilig...