Skip to content

Instantly share code, notes, and snippets.

@Steffo99
Created April 6, 2017 09:20
Show Gist options
  • Select an option

  • Save Steffo99/5000ccb2e13c2e5e3a88b5a00fa48575 to your computer and use it in GitHub Desktop.

Select an option

Save Steffo99/5000ccb2e13c2e5e3a88b5a00fa48575 to your computer and use it in GitHub Desktop.
#pragma once
#define NULL 0
template <typename T> class Node
{
private:
T data;
Node<T>* next;
public:
Node()
{
this->next = NULL;
}
Node(const Node<T>& old)
{
this->data = old.data;
this->next = old.next;
}
Node(T d)
{
this->data = d;
this->next = NULL;
}
T Data()
{
return this->data;
}
void Data(T d)
{
this->data = d;
}
Node<T>* Next()
{
return this->next;
}
void Next(Node<T>* n) {
this->next = n;
}
};
template <typename T> class Stack
{
private:
Node<T>* head;
public:
Stack()
{
this->head = NULL;
}
~Stack()
{
while(this->head != NULL)
{
Node<T>* current = this->head;
this->head = current->Next();
delete current;
}
}
bool Top(T& result)
{
if(this->head != NULL) {
result = this->head->Data();
return true;
}
else
{
return false;
}
}
void Push(T value)
{
Node<T>* n = new Node<T>(value);
n->Next(this->head);
this->head = n;
}
bool Pop(T& result)
{
if(this->head != NULL) {
result = this->head->Data();
delete this->head;
this->head = this->head->Next();
return true;
}
else
{
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment