Last active
December 16, 2015 17:09
-
-
Save opilar/5468107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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