C programs for Stack

#include<stdio.h>
#include<conio.h>
# define size 10

int push(int stack[],int tos,int val);
int pop(int stack[],int tos);
int display(int stack[],int tos);
int peep(int stack[],int tos);
int isempty(int tos);
int isfull(int tos);


void main()
{
 int stack[size],tos=-1,choice,ch=1,val;
 do
 {
 printf("\n 1.insert value in stack");
 printf("\n 2.display stack value");
 printf("\n 3.delete stack value");
 printf("\n 4.value of top position");
 printf("\n 5.stack is empty or not");
 printf("\n 6.stack is full or not");
 printf("\n\n enter your choice : ");
 scanf("%d",&choice);
 switch(choice)
 {
 case 1:  
      printf("\nenter value :: ");
   scanf("%d",&val);
        tos=push(stack,tos,val);
     break;
 case 2:
          display(stack,tos);
        break;
 case 3:
           tos=pop(stack,tos);
     break;
 case 4:
        peep(stack,tos);
     break;
 case 5:
     if(isempty(tos)==1)
     {
      printf("\n stack is empty");
     }
     else
     {
      printf("\n stack is not empty");
     }
     break;
 case 6:
     if(isfull(tos)==1)
     {
      printf("\n stack is full");
     }
     else
     {
      printf("\n stack is not full");
     }
     break;
 default:
      printf("\n invalid choice");
 }
 printf("\n do you want to continue enter(1) :: ");
 scanf("%d",&ch);
 }while(ch==1);
    getch();

}
int push(int stack[],int tos,int val)
{
 
  if(isfull(tos)==1)
  {
   printf("\n stack is full");
  }
  else
  {
   tos++;
   stack[tos]=val;
  }
 
 return tos;
}
int display(int stack[],int tos)
{
 
 int temp;
 temp=tos;
 if(temp==-1)
 {
  printf("\n stack is empty");
 }
 else
 {
    while(temp!=-1)
    {
    printf("\n%d",stack[temp]);
    temp--;
    }
 }
}
int pop(int stack[],int tos)
{
 
  if(isempty(tos)==1)
  {
   printf("\n stack is empty\n");
   
  }
  else
   { 
   printf("\ndeleted element : %d",stack[tos]);
            tos--;
  }
 return tos;
}
int peep(int stack[],int tos)
{
 
 if(tos==-1)
 {
  printf("\n stack is empty\n");
 }
 else
 {
  printf("\n%d",stack[tos]);
 }
}
int isempty(int tos)
{
 
 if(tos==-1)
 { 
  return 1;
 }
 else
 {
  return 0;
 }
}
int isfull(int tos)
{
 if(tos==size-1)
 {
  return 1;
 }
 else
 {
  return 0;
 }
}

***********************************************************************************************************************
------------------------------------------------------------------------

         output

------------------------------------------------------------------------


 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 1

enter value :: 10

 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 1

enter value :: 20

 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 1

enter value :: 30

 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 1

enter value :: 40

 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 1

enter value :: 50

 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 2

50
40
30
20
10
 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 3

deleted element : 50
 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 4

40
 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 5

 stack is not empty
 do you want to continue enter(1) :: 1

 1.insert value in stack
 2.display stack value
 3.delete stack value
 4.value of top position
 5.stack is empty or not
 6.stack is full or not

 enter your choice : 6

 stack is not full
 do you want to continue enter(1) ::

**********************************************************************************************************************************
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment