Create a class called Stack for storing integers. The data members are an integer array for storing
the integers and an integer for storing the top of stack (tos). Include member functions for initializing tos
to 0, pushing an element to the stack and for popping an element from the stack. The push() function
should check for “stack overflow” and pop() should check for “stack underflow”.
#include<iostream>
using namespace std;
#define SIZE 50
class Stack {
int arr[SIZE];
int tos;
public:
Stack() {
tos = 0;
}
push(int value) {
if (tos < SIZE) {
arr[tos++] = value;
}
else
cout << "Stack overflow" << endl;
}
int pop() {
if (tos == 0) {
cout << "Stack underflow " << endl;
}
else {
return arr[--tos];
}
}
set(int a) {
tos = a;
}
};
int main() {
Stack s;
for (int i = 0; i < 50; i++) {
s.push(i);
}
for (int i = 0; i < 51; i++) {
cout << s.pop() << " " << i << endl;
}
}
Comments
Leave a comment