int PUSH ( int x, int TOS , int stack[ ],int size)
{
if(TOS == size-1)
{
printf("Stack Overflow");
}
else
{
TOS++;
stack[TOS]=x;
}
return(TOS);
}
int POP(int TOS,int stack[ ])
{
int x;
if(TOS==-1)
{
printf("Stack under flow");
}
else
{
x=stack[TOS];
TOS--;
printf("POP element is %d\n",x);
}
return(TOS);
}
void display(int TOS,int stack[ ])
{
int i;
printf("Element of STACK:\n");
for(i=TOS;i>=0;i--)
{
printf("\n%d",stack[i]);
}
}
int ISEMPTY ( int TOS )
{
if (TOS==-1)
{
return(1);
}
else
{
return(0);
}
}
PROCDURE IN C FOR CHECKING STACK ISFULL
int ISFULL ( int TOS , int size )
{
if(TOS==size-1)
{
return(1);
}
else
{
return(0);
}
}
PROCDURE IN C FOR STACK TOP
It return top most element of stack.
int stack top ( )
{
return (stack[TOS] );
}
PROCEDURE IN C FOR PEEP
It return element from any particular position
int peep (int pos)
{
if (pos <=TOS +1)
{
return (stack[TOS - pos+1]);
}
else
{
printf("Invalid Position ! ");
}
Complete Program of implementing Stack
#include <conio.h>
#include <stdio.h>
int PUSH ( int x, int TOS , int stack[ ],int size)
{
if(TOS == size-1)
{
printf("Stack Overflow");
}
else
{
TOS++;
stack[TOS]=x;
//printf("%d",stack[TOS]);
}
return(TOS);
}
int POP(int TOS,int stack[ ])
{
int x;
if(TOS==-1)
{
printf("Stack under flow");
}
else
{
x=stack[TOS];
TOS--;
printf("POP element is %d\n",x);
}
return(TOS);
}
void display(int TOS,int stack[ ])
{
int i;
printf("Element of STACK:\n");
for(i=TOS;i>=0;i--)
{
printf("\n%d",stack[i]);
}
}
int ISEMPTY ( int TOS )
{
if (TOS==-1)
{
return(1);
}
else
{
return(0);
}
}
int ISFULL ( int TOS , int size )
{
if(TOS==size-1)
{
return(1);
}
else
{
return(0);
}
}
void main()
{
int x,TOS=-1,size,stack[10],ch=0;
printf("Enter the size of stack:");
scanf("%d",&size);
printf("\n");
do
{
printf("\nSelect Operation\n");
printf("1-PUSH 2-POP 3-Display 0-Exit:");
scanf("%d",&ch);
switch (ch)
{
case 1:
{
int f;
f=ISFULL(TOS,size);
if(f==0)
{
printf("Enter the element PUSH into stack:");
scanf("%d",&x);
TOS=PUSH(x,TOS,stack,size);
display(TOS,stack);
}
else
{
printf("Stack is full !");
}
break;
}
case 2:
{
int e;
e=ISEMPTY(TOS);
if(e==0)
{
TOS=POP(TOS,stack);
display(TOS,stack);
}
else
{
printf("Stack is empty!");
}
break;
}
case 3:
{
display(TOS,stack);
break;
}
default :
printf("NONE");
}
} while(ch!=0);
getch();
}
No comments:
Post a Comment