Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STACK_HPP
- #define STACK_HPP
- #include <iostream>
- using namespace std;
- struct link
- {
- int data;
- link *next;
- };
- class Stack
- {
- private:
- link *first = nullptr;
- int count = 0;
- public:
- Stack();
- ~Stack();
- void push(const int number);
- int top();
- void pop();
- void removeStack();
- int size();
- bool empty();
- void print();
- };
- #endif //STACK_HPP
- .........................................................................................................................
- #include "Stack.hpp"
- Stack::Stack()
- {
- count = 0;
- }
- Stack::~Stack()
- {
- while(first){
- link *current = first;
- first = current->next;
- delete current;
- }
- }
- void Stack::push(int number)
- {
- link *newElement = new link;
- newElement->data = number;
- newElement->next = first;
- first = newElement;
- count++;
- }
- int Stack::top()
- {
- if(first)
- return first->data;
- cout << "\nStack is empty" << endl;
- return 0;
- }
- void Stack::pop()
- {
- if(count){
- link *current = first;
- first = current->next;
- delete current;
- count--;
- }else{
- cout << "\nStack is empty" << endl;
- }
- }
- void Stack::removeStack()
- {
- while(first){
- link *current = first;
- first = current->next;
- delete current;
- }
- count = 0;
- cout << "\nStack successfully cleared" << endl;
- }
- int Stack::size()
- {
- return count;
- }
- bool Stack::empty()
- {
- return (count) ? true : false;
- }
- void Stack::print()
- {
- while(first){
- link *current = first;
- first = current->next;
- cout << current->data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement