A limitedsized Stack ADT is an abstract data type that has a
Solution
#include<stdio.h>
#include<stdlib.h>
const int N =10;
typedef struct
{
int data[10];//array of at most size n
int top;
int bottom;
}stack_t;
void print(stack_t t)//prints the stack from top to bottom
{
printf(\"The stack:\ \");
int i=t.top;
printf(\"%d\ \",t.data[t.top]);
while(i>=t.bottom)
{
printf(\"%d\ \",t.data[i]);
i--;
}
}
void push(stack_t *t,int k)//pushes the integer k to stack
{
if(t->top+1<N){
t->top++;
t->data[t->top]=k;
}
else
{
printf(\"\ Error stack is full\ \");
}
}
int pop(stack_t *t)//pops top element from stack
{
if(t->top==-1){
printf(\"\ Error : stack is empty\ \");
return -1;}
int k =t->data[t->top];
t->top--;
return k;
}
int isEmpty(stack_t t)//checks whether stack is empty or not
{
if(t.top==-1)return 1;
return 0;
}
int height(stack_t t)//returns height of the stack
{
return t.top+1;
}
int peek(stack_t t)//returns top element of the stack
{
return t.data[t.top];
}
int main()
{
stack_t T;
T.top=-1;
T.bottom=0;
push(&T,2);//pushing to array
push(&T,3);//pushing to array
push(&T,1);//pushing to array
print(T);//printing stack
pop(&T);//POPING from stack successfull
printf(\"\ Height of the stack:%d\ \",height(T));//finding height of stack
printf(\"Top element of the stack:%d\ \",peek(T));//printing top element
pop(&T);//successfull
pop(&T);//successfull
pop(&T);//unsuccessfull*/
return 0;
}
output:-
The stack:
1
3
2
Height of the stack:2
Top element of the stack:3
Error : stack is empty
Process exited normally.
Press any key to continue . . .

