Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #ifndef STACK_HPP
  2. #define STACK_HPP
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. struct link
  8. {
  9.     int data;
  10.     link *next;
  11. };
  12.  
  13. class Stack
  14. {
  15. private:
  16.     link *first = nullptr;
  17.     int count = 0;
  18.    
  19. public:
  20.     Stack();
  21.     ~Stack();
  22.     void push(const int number);
  23.     int top();
  24.     void pop();
  25.     void removeStack();
  26.     int size();
  27.     bool empty();
  28.     void print();
  29. };
  30.  
  31. #endif //STACK_HPP
  32. .........................................................................................................................
  33. #include "Stack.hpp"
  34.  
  35. Stack::Stack()
  36. {
  37.     count = 0;
  38. }
  39.  
  40. Stack::~Stack()
  41. {
  42.     while(first){
  43.         link *current = first;
  44.         first = current->next;
  45.         delete current;
  46.     }
  47. }
  48.  
  49. void Stack::push(int number)
  50. {
  51.     link *newElement = new link;
  52.     newElement->data = number;
  53.     newElement->next = first;
  54.     first = newElement;
  55.  
  56.     count++;
  57. }
  58.  
  59. int Stack::top()
  60. {
  61.     if(first)
  62.         return first->data;
  63.    
  64.     cout << "\nStack is empty" << endl;
  65.  
  66.     return 0;
  67. }
  68.  
  69. void Stack::pop()
  70. {
  71.     if(count){
  72.         link *current = first;
  73.         first = current->next;
  74.         delete current;
  75.         count--;
  76.     }else{
  77.         cout << "\nStack is empty" << endl;
  78.     }
  79. }
  80.  
  81. void Stack::removeStack()
  82. {
  83.     while(first){
  84.         link *current = first;
  85.         first = current->next;
  86.         delete current;
  87.     }
  88.  
  89.     count = 0;
  90.     cout << "\nStack successfully cleared" << endl;
  91. }
  92.  
  93. int Stack::size()
  94. {
  95.     return count;
  96. }
  97.  
  98. bool Stack::empty()
  99. {
  100.     return (count) ?  true : false;
  101. }
  102.  
  103. void Stack::print()
  104. {
  105.     while(first){
  106.         link *current = first;
  107.         first = current->next;
  108.         cout << current->data;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement