Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Stack {
- int stackSize;
- int stack[stackSize];
- int tos;
- public:
- Stack(int sz) {
- tos = 0;
- stackSize = sz;
- stack[sz] = {0};
- }
- void push(int n){
- if (tos < stackSize){
- if (tos == stackSize - 1) {
- stack[tos] = n;
- } else {
- stack[tos] = n;
- tos++;
- }
- } else if (tos > stackSize - 1) {
- cout << "STACK OVERFLOW! CANNOT PUSH\n";
- tos = stackSize - 1;
- }
- }
- void pop(){
- int data = 0;
- if (tos == -1){
- cout << "STACK UNDERFLOW! CANNOT POP\n";
- tos = 0;
- } else if (tos == 0) {
- data = stack[0];
- cout << "Data: " << data << " has been popped\n";
- } else {
- data = stack[tos];
- cout << "Data: " << data << "has been popped\n";
- tos--;
- }
- }
- void display(){
- for (int i = tos - 1; i >= 0; i--){
- cout << stack[tos] << endl;
- }
- }
- };
- int main(){
- Stack ds(5);
- ds.push(1); //tos = 0
- ds.push(2); // 1
- ds.push(3);
- ds.push(4);
- ds.push(5);
- ds.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment