Initial commit of code for dopelib, and ch 1, 2, and 12
This commit is contained in:
59
libs/dope/stacks.hpp
Normal file
59
libs/dope/stacks.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
template <class T, size_t S>
|
||||
class DopeStack{
|
||||
static const size_t size = S;
|
||||
T stack[S];
|
||||
T* head;
|
||||
size_t occupancy;
|
||||
public:
|
||||
DopeStack();
|
||||
T pop();
|
||||
void push(T item);
|
||||
T peek();
|
||||
size_t getOccupancy(){return occupancy;}
|
||||
bool isEmpty();
|
||||
};
|
||||
|
||||
// Implementation ____________________________________________________
|
||||
|
||||
template <class T, size_t S>
|
||||
DopeStack<T, S>::DopeStack():
|
||||
head (&stack[0]),
|
||||
occupancy(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
template <class T, size_t S>
|
||||
T DopeStack<T, S>::pop(){
|
||||
T ret = *head;
|
||||
if(head != &stack[0]){
|
||||
head--;
|
||||
}
|
||||
|
||||
occupancy--;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <class T, size_t S>
|
||||
void DopeStack<T, S>::push(T item){
|
||||
if(occupancy != 0){
|
||||
head++;
|
||||
}
|
||||
|
||||
*head = item;
|
||||
occupancy++;
|
||||
}
|
||||
|
||||
template <class T, size_t S>
|
||||
T DopeStack<T, S>::peek(){
|
||||
return *head;
|
||||
}
|
||||
|
||||
template <class T, size_t S>
|
||||
bool DopeStack<T, S>::isEmpty(){
|
||||
return (occupancy == 0);
|
||||
}
|
||||
Reference in New Issue
Block a user