stack using structure

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

void push(struct stack *s,int val);
void pop(struct stack *s);
void display(struct stack s);
int peep(struct stack *s,int tos);
int isempty(int tos);
int isfull(int tos);

struct stack
{
 int arr[size];
 int tos;
};

void main()
{
 struct stack s;
 int choice,ch=1,val;
 s.tos=-1;
 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);
   push(&s,val);
     break;
 case 2:
  display(s);
        break;
 case 3:
     pop(&s);
     break;
 case 4:
  peep(&s,s.tos);
     break;
 case 5:
  if(isempty(s.tos)==1)
     {
      printf("\n stack is empty");
     }
     else
     {
      printf("\n stack is not empty");
     }
     break;
 case 6:
  if(isfull(s.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();

}
void push(struct stack *s,int val)
{
 
 if(s->tos==size-1)
  {
   printf("\n stack is full");
  }
  else
  {
   ++s->tos;
   s->arr[s->tos]=val;
  }
}
void display(struct stack s)
{
 
 int i;
 if(s.tos==-1)
 {
  printf("\n stack is empty");
 }
 else
 {
  for(i=s.tos;i>=0;i--)
    {
     printf("\n%d",s.arr[i]);
    //temp--;
    }
 }
}
void pop(struct stack *s)
{
 
 if(s->tos==-1)
  {
   printf("\n stack is empty\n");
   
  }
  else
   { 
   printf("\ndeleted element : %d",s->arr[s->tos]);
   s->tos--;
  }
  
}
int peep(struct stack *s,int tos)
{
 
 if(s->tos==-1)
 {
  printf("\n stack is empty\n");
 }
 else
 {
  printf("\n%d",s->arr[tos]);
 }
}
int isempty(int tos)
{
 int temp;
 temp=tos;
 if(temp==-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 :: 15

 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 :: 25

 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 : 2

30
25
20
15
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 : 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 : 4

25
 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) :: 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

25
20
15
10
 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