/*
* A simple stack implementation (without templates)
*
* Description:
* A simple stack implementation that accepts integers as its datatype.
* Users are able to push to the back, pop from the back, and access the
* top element in the same way as they would with STL's stack.
*
* Author:
* Clara Nguyen
*/
#include <iostream>
#include "stack.hpp"
using namespace std;
//Node Struct
cn_stack::node::node() {
next = prev = (node *)0x0;
}
//CN_Stack stuff
cn_stack::cn_stack() {
//Make sentinel and make it point to itself
sentinel = new node();
sentinel->prev = sentinel->next = sentinel;
N = 0;
}
cn_stack::~cn_stack() {
clear();
delete sentinel;
}
void cn_stack::clear() {
while (N) {
pop();
}
}
void cn_stack::push(const int &i) {
node *p = new node();
p->data = i;
//Get the top of the list.
node *end = sentinel->prev;
p->prev = sentinel->prev;
p->next = sentinel;
end->next = p;
sentinel->prev = p;
N++;
}
void cn_stack::pop() {
node *p = sentinel->prev;
sentinel->prev = p->prev;
sentinel->prev->next = sentinel;
delete p;
N--;
}
int cn_stack::size() {
return N;
}
int cn_stack::top() {
return sentinel->prev->data;
}
void cn_stack::debug_print() {
node *c = sentinel;
cout << "Stack size: " << size() << endl;
for (int i = 0; i < N; i++) {
c = c->next;
cout << c->data << endl;
}
}