Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. // ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. #define MAX_SIZE 10
  11.  
  12. typedef struct Stack {
  13. int _data[MAX_SIZE];
  14. unsigned int _top = 0;
  15.  
  16. bool isEmpty() {
  17.  
  18. if (_top == 0) return true;
  19. else return false;
  20.  
  21. }
  22.  
  23. bool isFull() {
  24.  
  25. if (_top == MAX_SIZE) return true;
  26. else return false;
  27.  
  28. }
  29.  
  30.  
  31. void push(int newVal) {
  32.  
  33.  
  34. if (!isFull()) {
  35. _data[_top] = newVal;
  36. _top++;
  37. }else cout<<"Stack is Full"<<endl;
  38. }
  39.  
  40. void pop() {
  41.  
  42. if (!isEmpty()) {
  43. _top--;
  44. }
  45. else {
  46. cout << "Stack is Empty" << endl;
  47. }
  48.  
  49. }
  50.  
  51. int top() {
  52.  
  53. if (!isEmpty()) {
  54. return _data[_top - 1];
  55. }
  56. else {
  57. cout << "Stack is Empty" << endl;
  58. }
  59.  
  60. }
  61.  
  62. int size() {
  63.  
  64. return _top;
  65.  
  66. }
  67.  
  68.  
  69. void reset() {
  70.  
  71. _top = 0;
  72.  
  73. }
  74.  
  75.  
  76. void print() {
  77.  
  78. int temp = _top - 1;
  79.  
  80. while (temp >= 0) {
  81. cout << _data[temp] << endl;
  82. temp--;
  83. }
  84.  
  85. }
  86.  
  87. };
  88.  
  89.  
  90. int main() {
  91.  
  92. cout << "test" << endl;
  93.  
  94. Stack s1;
  95. s1.reset();
  96. s1.print();
  97. s1.push(3);
  98. s1.push(5);
  99. cout << "Top:" << s1.top() << endl;
  100. s1.push(2);
  101. cout << "Top:" << s1.top() << endl;
  102. s1.print();
  103. s1.pop();
  104. s1.print();
  105. s1.push(1);
  106. cout << "Top:" << s1.top() << endl;
  107. s1.print();
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement