Skip to content

Instantly share code, notes, and snippets.

@opilar
Last active December 16, 2015 17:09
Show Gist options
  • Save opilar/5468107 to your computer and use it in GitHub Desktop.
Save opilar/5468107 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
struct Elem
{
Elem(int _v, Elem* _p) { v = _v; p = _p; };
int v;
Elem* p;
};
class Stack
{
public:
Stack() { t = 0; };
~Stack() { clear(); };
void push(int);
int pop();
int top() const { return t->v; };
bool isEmpty() const { return t == 0; };
void clear();
private:
Elem* t;
};
void Stack::push(int v)
{
Elem* p = new Elem(v, t);
t = p;
}
int Stack::pop()
{
Elem* p = t;
t = t->p;
int v = p->v;
delete p;
return v;
}
void Stack::clear()
{
while(!isEmpty())
pop();
}
int main()
{
Stack stk;
for (int i = 1; i <= 10; i++)
stk.push(i);
while (!stk.isEmpty())
cout << stk.pop() << endl;
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment