#include<stdio.h>
const int MAX_SIZE = 100;
int stack[100];
int top=-1;
int push(int);
int pop();
int isEmpty();
int isFull();
int peek();
int count();
int change(int,int);
int display();
void main(){
push(1);
push(2);
push(3);
display();
pop();
display();
printf("Peek %d\n",peek());
printf("Count %d\n",count());
printf("IsFull %d\n",isFull());
printf("IsEmpty %d\n",isEmpty());
printf("Change at pos 2",change(2,9));
display();
}
int push(int item){
stack[++top] = item;
return top;
}
int pop(){
return stack[top--];
}
int isEmpty(){
return (top<=-1)? 1 : 0;
}
int isFull(){
return (top>=MAX_SIZE)? 1 : 0;
}
int peek(){
return stack[top];
}
int count(){
return top+1;
}
int change(int pos,int data){
if (pos<top && pos>-1)
{
stack[pos] = data;
return 1;
}
return 0;
}
int display(){
for (int i = 0; i <= top; i++)
{
printf(" %d, ",stack[i]);
}
printf("\n");
return 1;
}
Input :
push(1);
push(2);
push(3);
display();
pop();
display();
printf("Peek %d\n",peek());
printf("Count %d\n",count());
printf("IsFull %d\n",isFull());
printf("IsEmpty %d\n",isEmpty());
printf("Change at pos 2",change(2,9));
display();
Output:
1, 2, 3,
1, 2,
Peek 2
Count 2
IsFull 0
IsEmpty 0
Change at pos 2 1, 2,
Comments
Post a Comment