This is my code :
#include <iostream>
using namespace std;
#define MAX 5
The structure is defined :
struct Stack{
int data[MAX];
int top;
};
Checks whether the stack is full :
bool isFull(int tmpTop){
return (tmpTop==(MAX-1));
}
Checks whether the stack is empty :
bool isEmpty(int tmpTop){
return (tmpTop==-1);
}
Adds data to the stack :
void push(Stack *ptrStack, int n){
if(isFull(ptrStack->top)){
cout<<"Stack is full! Push aborted!\n";
}else {
ptrStack->top++;
ptrStack->data[ptrStack->top]=n;
}
}
Gets the size of the stack :
int getSize(int tmpTop){
return tmpTop+1;
}
Displays the stack :
void displayStack(Stack tmpStack){
if(isEmpty(tmpStack.top)){
cout<<"Stack is empty! Displaystack is aborted!\n";
}else {
for(int i=0;i<=tmpStack.top;i++){
cout<<tmpStack.data[i]<<" ";
}
}
}
Returns the value in the stack :
int pop(Stack *ptrStack){
int data = -1 ;
if(isEmpty(ptrStack->top)){
cout<<"Stack is empty! Pop is aborted!\n";
}else {
data=ptrStack->data[ptrStack->top];
ptrStack->top--;
}
return data;
}
The main method :
int main(){
Stack aStack;
push(&aStack,11);
push(&aStack,22);
push(&aStack,33);
displayStack(aStack);
return 0;
}