Skip to content

Instantly share code, notes, and snippets.

@teknoman117
Last active February 26, 2016 06:46
Show Gist options
  • Save teknoman117/2316efacbccb7e059851 to your computer and use it in GitHub Desktop.
Save teknoman117/2316efacbccb7e059851 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "Stack.h"
using namespace std;
int main (int argc, char **argv)
{
int N;
Stack stack;
stack.initialize();
cin >> N;
for(int i = 0; i < N; i++)
{
double val;
cin >> val;
stack.push(new double(val));
}
// Print out everything
Stack::Link *current = stack.head;
while(current != NULL)
{
double *val = static_cast<double *>(current->data);
cout << *val << endl;
current = current->next;
}
void *datum = NULL;
while((datum = stack.pop()) != NULL)
{
delete static_cast<double *>(datum);
}
stack.cleanup();
return 0;
}
#ifndef __LINKEDLIST__
#define __LINKEDLIST__
#include <iostream>
struct LinkedList
{
struct Link
{
void* data;
Link* next;
Link(void* dat, Link* nxt)
{
data = dat;
next = nxt;
}
}* head;
LinkedList(void* dat, Link* nxt)
{
head = new Link(dat, nxt);
}
void add(LinkedList::Link* l, int n)
{
for(int i = n; i > 0; i--)
{
l->next = new Link(new double(i-1), l->next);
}
}
void print()
{
Link* current = head;
while(current != NULL)
{
std::cout << *static_cast<double *>(current->data) << std::endl;
current = current->next;
}
}
void cleanup()
{
Link* current = head;
while(current != NULL)
{
delete static_cast<double *>(current->data);
Link* n = current->next;
delete current;
current = n;
}
head = NULL;
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment