In: Computer Science
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions.
C++
C++ code
#include<iostream>
using namespace std;
class IntStack{
private:
int *arr;
int top,capacity;
public:
IntStack(int cap){
top = -1;
capacity =
cap;
arr = new
int[capacity];
}
~IntStack(){
delete []
arr;
}
bool isFull(){
return top ==
capacity - 1;
}
bool isEmpty(){
return top ==
-1;
}
void push(int data){
if(!isFull()){
arr[++top] = data;
}
}
int pop(){
if(!isEmpty()){
int data = arr[top];
top--;
return data;
}
}
};
int main(){
IntStack stack(10);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
cout<<"Stack: ";
while(!stack.isEmpty()){
cout<<stack.pop()<<"
";
}
return 0;
}