Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <exception>
- #define myStack
- #define myAlg
- using namespace std;
- #ifdef myStack
- template<class T>
- class Stack {
- struct Node {
- Node(T el, Node * node)
- {
- data = el;
- next = node;
- }
- Node* next;
- T data;
- };
- Node* _top;
- int _size;
- public:
- Stack()
- {
- _top = NULL;
- _size = 0;
- }
- ~Stack()
- {
- while (_top != NULL && _top->next != NULL)//_top != NULL <=> _size > 0
- {
- Node* temp = _top;
- _top = _top->next;
- delete temp;
- }
- }
- void push(T el);
- void pop();
- T top();
- int size();
- T peek();
- bool empty();
- };
- #endif
- #ifdef myAlg
- template<class T>
- bool Stack<T>::empty()
- {
- return !_size;
- }
- template<class T>
- T Stack<T>::peek()
- {
- if (!_size)
- return NULL;
- return _top->data;
- }
- template<class T>
- int Stack<T>::size()
- {
- return _size;
- }
- template<class T>
- T Stack<T>::top()
- {
- try
- {
- if (_top == NULL)
- throw out_of_range("Error: Stack is empty!");
- return _top->data;
- }
- catch (exception &e)
- {
- cout << e.what() << endl;
- }
- }
- template<class T>
- void Stack<T>::pop() {
- Node* temp = _top;
- _top = _top->next;
- delete temp;
- _size--;
- }
- template<class T>
- void Stack<T>::push(T el)
- {
- Node * temp = new Node(el, _top);
- _top = temp;
- _size++;
- }
- #endif // myAlg
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement