Created
October 15, 2012 08:16
-
-
Save NonLogicalDev/3891391 to your computer and use it in GitHub Desktop.
Stack Template -- My stab at purty coding.
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
// Lab Lab 8a (Calculator8) | |
// Programmer: Oleg Utkin | |
// Editor(s) used: Xcode | |
// Compliler(s) used: Xcode | |
// | |
#include <iostream> | |
#ifndef NULL | |
#define NULL reinterpret_cast<void *>(0) | |
#endif | |
#define self (*this) //'Objective-C'-escue this pointer | |
//Node Declaration | |
#ifndef Lab_8a__Calculator8__StackNode_cpp | |
#define Lab_8a__Calculator8__StackNode_cpp | |
template <class datatype> | |
struct Lab8StackNode { | |
datatype data; | |
Lab8StackNode *next; | |
Lab8StackNode() {} | |
Lab8StackNode(datatype data, Lab8StackNode *next): data(data), next(next) {} | |
}; | |
#endif | |
//Smart Iterator Declaration | |
#ifndef Lab_8a__Calculator8__StackIterator_cpp | |
#define Lab_8a__Calculator8__StackIterator_cpp | |
template <class datatype> | |
class Lab8StackIterator { | |
datatype *thisItem; | |
public: | |
//Constructors | |
Lab8StackIterator(): thisItem( NULL ) {} | |
Lab8StackIterator(datatype *iteratorPointer): thisItem(iteratorPointer) {} | |
//Iterator function | |
datatype* next() { return thisItem = (thisItem) ? (thisItem->next) : NULL; } | |
//Accessors & Quasi-Mutators | |
datatype& getRef() { return *thisItem; } | |
datatype* getPointer() { return thisItem; } | |
const datatype& getRef() const { return *thisItem; } | |
const datatype* getPointer() const { return thisItem; } | |
//Mutators | |
datatype* operator=(datatype* newIterator) { return thisItem = newIterator; } | |
//Typecasts | |
operator datatype() { return *thisItem; } | |
operator datatype*() { return thisItem; } | |
}; | |
#endif | |
//Stack Declaration | |
#ifndef Lab_8a__Calculator8__Stack_cpp | |
#define Lab_8a__Calculator8__Stack_cpp | |
template <class datatype> | |
class Stack { | |
//Type declarations | |
typedef Lab8StackNode<datatype> Node; | |
typedef Lab8StackIterator<Node> iterator; | |
//Data members | |
static Node ndef; | |
Node* start; | |
public: | |
Stack(); | |
Stack(const Stack& source); | |
~Stack(); | |
//Accessors | |
bool empty() const { return (start) ? false : true; } | |
const datatype& top() const; | |
datatype& top(); | |
//Mutators | |
void push(datatype newElement); | |
void pop(); | |
//Overloaded operators | |
Stack& operator=(const Stack& source); | |
}; | |
template <class datatype> | |
typename Stack<datatype>::Node Stack<datatype>::ndef; //Initiating dummy node | |
//Constructors | |
template <class datatype> | |
Stack<datatype>::Stack(): start(0) {} | |
template <class datatype> | |
Stack<datatype>::Stack(const Stack& source): start(0) { | |
iterator thisItem = source.start; | |
while ( thisItem ) { | |
self.push( thisItem.getRef().data ); | |
thisItem.next(); | |
} | |
} | |
//Destructor | |
template <class datatype> | |
Stack<datatype>::~Stack() { | |
iterator thisItem = start, prevItem; | |
while (( prevItem = thisItem.next() )) | |
delete prevItem.getPointer(); | |
} | |
//Acessors | |
template <class datatype> | |
const datatype& Stack<datatype>::top() const { | |
return ( (start) ? start->data: ndef.data ); | |
} | |
template <class datatype> | |
datatype& Stack<datatype>::top() { | |
return ( (start) ? start->data : ndef.data ); | |
} | |
//Mutators | |
template <class datatype> | |
void Stack<datatype>::push(datatype newElement) { | |
Node* newNode = new Node( datatype(newElement), start ); | |
start = newNode; | |
} | |
template <class datatype> | |
void Stack<datatype>::pop() { | |
Node* newStart = start->next; | |
delete [] start; | |
start = newStart; | |
} | |
template <class datatype> | |
Stack<datatype>& Stack<datatype>::operator=(const Stack<datatype>& source){ | |
iterator thisItem = source.start; | |
while ( thisItem ) { | |
self.push( thisItem.getRef().data ); | |
thisItem.next(); | |
} | |
return self; | |
} | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment