C++ #include #include using namespace std; class Stack { private: vector vec; p
Posted: Sun Jul 10, 2022 11:23 am
C++
#include <iostream>#include <vector>
using namespace std;
class Stack { private: vector<int> vec; public: void push(int n) { vec.push_back(n); }
int pop() { int n = vec[vec.size() -1]; vec.pop_back(); return n; }
int top() { return vec[vec.size() -1]; }
bool isEmpty() { return vec.empty(); }
int size() { return vec.size(); } };
int main() { Stack s; for (int i = 0; i < 5; ++i) { s.push(i); } cout << "Stack contains" <<endl; while (!s.isEmpty()) { cout << s.pop()<< endl; } return 0; }
Repeat the following programming problem above, butafter the first doubling of the array, halve the size of the arrayif fewer than half of the array’s locations are occupied by currentstack entries.
#include <iostream>#include <vector>
using namespace std;
class Stack { private: vector<int> vec; public: void push(int n) { vec.push_back(n); }
int pop() { int n = vec[vec.size() -1]; vec.pop_back(); return n; }
int top() { return vec[vec.size() -1]; }
bool isEmpty() { return vec.empty(); }
int size() { return vec.size(); } };
int main() { Stack s; for (int i = 0; i < 5; ++i) { s.push(i); } cout << "Stack contains" <<endl; while (!s.isEmpty()) { cout << s.pop()<< endl; } return 0; }
Repeat the following programming problem above, butafter the first doubling of the array, halve the size of the arrayif fewer than half of the array’s locations are occupied by currentstack entries.