#include<stdio.h>
void PUSH(int s[],int *top,int num)
{ int i;
if(*top == 4)
{
printf("\nStack Overflow...");
return;
}
else
{
(*top)++;
s[*top]=num;
for(i=(*top);i>=0;i--)
{
printf("\n| %d |",s[i]);
}
}
}
int POP(int s[],int *top)
{ int temp;
if(*top==-1)
{
return 0;
}
else
{ temp=s[*top];
(*top)--;
return temp;
}
}
void display(int s[],int *top)
{
if(*top==-1)
{
printf("\nStack is Empty...");
}
else
{
int i;
printf("Your stack : ");
for(i=(*top);i>=0;i--)
{
printf("\n| %d |",s[i]);
}
}
}
int peep(int s[],int *top,int i)
{ if(i == 0)
{
return 0;
}
else
{
if(*top-i+1 < 0)
{
return 0;
}
else
{
return s[*top-i+1];
}
}
}
void change(int s[],int *top,int i,int num)
{ if(i == 0)
{
printf("\nInvalid Position...");
}
else
{
if(*top-i+1 < 0)
{
printf("\nInvalid Position...");
}
else
{
s[*top-i+1]=num;
printf("\nValue changed Successfully");
display(s,top);
}
}
}
void isEmpty(int *top)
{
if(*top == -1)
printf("\nStack is EMPTY.");
else
printf("\nStack is NOT EMPTY.");
}
void isFull(int *top)
{
if(*top == 4)
printf("Stack is FULL.");
else
printf("Stack is NOT FULL.");
}
void main()
{
int s[4],pop,pep;
int t=-1;
int *top=&t;
int opt,num,i;
clrscr();
again:
printf("\n\n------------------------");
printf("\n1. PUSH\n2. POP\n3. PEEP\n4. CHANGE\n5. DISPLAY\n6. ISEMPTY\n7. ISFULL\n8. EXIT");
printf("------------------------\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter number to PUSH : ");scanf("%d",&num);
PUSH(s,top,num);
goto again;
break;
case 2: pop=POP(s,top);
if(pop == 0)
printf("Stack Underflow...");
else
printf("\nElement Poped : %d",pop);
goto again;
break;
case 3: printf("\nEnter the position : ");
scanf("%d",&i);
pep=peep(s,top,i);
if(pep == 0)
printf("Invalid Position...");
else
printf("\nValue at position %d is : %d",i,pep);
goto again;
break;
case 4: printf("\nEnter the position: ");scanf("%d",&i);
printf("\nEnter the number: ");scanf("%d",&num);
change(s,top,i,num);
goto again;
break;
case 5: display(s,top);
goto again;
break;
case 6: isEmpty(top);
goto again;
break;
case 7: isFull(top);
goto again;
break;
case 8: goto exit;
break;
default: printf("Invalid Choice...");
goto again;
break;
}
exit:
}
void PUSH(int s[],int *top,int num)
{ int i;
if(*top == 4)
{
printf("\nStack Overflow...");
return;
}
else
{
(*top)++;
s[*top]=num;
for(i=(*top);i>=0;i--)
{
printf("\n| %d |",s[i]);
}
}
}
int POP(int s[],int *top)
{ int temp;
if(*top==-1)
{
return 0;
}
else
{ temp=s[*top];
(*top)--;
return temp;
}
}
void display(int s[],int *top)
{
if(*top==-1)
{
printf("\nStack is Empty...");
}
else
{
int i;
printf("Your stack : ");
for(i=(*top);i>=0;i--)
{
printf("\n| %d |",s[i]);
}
}
}
int peep(int s[],int *top,int i)
{ if(i == 0)
{
return 0;
}
else
{
if(*top-i+1 < 0)
{
return 0;
}
else
{
return s[*top-i+1];
}
}
}
void change(int s[],int *top,int i,int num)
{ if(i == 0)
{
printf("\nInvalid Position...");
}
else
{
if(*top-i+1 < 0)
{
printf("\nInvalid Position...");
}
else
{
s[*top-i+1]=num;
printf("\nValue changed Successfully");
display(s,top);
}
}
}
void isEmpty(int *top)
{
if(*top == -1)
printf("\nStack is EMPTY.");
else
printf("\nStack is NOT EMPTY.");
}
void isFull(int *top)
{
if(*top == 4)
printf("Stack is FULL.");
else
printf("Stack is NOT FULL.");
}
void main()
{
int s[4],pop,pep;
int t=-1;
int *top=&t;
int opt,num,i;
clrscr();
again:
printf("\n\n------------------------");
printf("\n1. PUSH\n2. POP\n3. PEEP\n4. CHANGE\n5. DISPLAY\n6. ISEMPTY\n7. ISFULL\n8. EXIT");
printf("------------------------\n >> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter number to PUSH : ");scanf("%d",&num);
PUSH(s,top,num);
goto again;
break;
case 2: pop=POP(s,top);
if(pop == 0)
printf("Stack Underflow...");
else
printf("\nElement Poped : %d",pop);
goto again;
break;
case 3: printf("\nEnter the position : ");
scanf("%d",&i);
pep=peep(s,top,i);
if(pep == 0)
printf("Invalid Position...");
else
printf("\nValue at position %d is : %d",i,pep);
goto again;
break;
case 4: printf("\nEnter the position: ");scanf("%d",&i);
printf("\nEnter the number: ");scanf("%d",&num);
change(s,top,i,num);
goto again;
break;
case 5: display(s,top);
goto again;
break;
case 6: isEmpty(top);
goto again;
break;
case 7: isFull(top);
goto again;
break;
case 8: goto exit;
break;
default: printf("Invalid Choice...");
goto again;
break;
}
exit:
}
No comments:
Post a Comment