Check Whether Stack is Empty or Not ?



Check Whether Stack is Empty or Not ?

We are using Empty Function for Checking whether stack is empty or not -

  1. Function returns “True” if Stack is Empty.
  2. Function returns “False” if Stack is Non-Empty.
  3. Function Takes “Pointer to Stack”
int empty (stack *s)
  1. Return Type : Integer. [Empty Stack Return 1 , Non Empty Stack Return 0 ]
  2. Parameter : Address of Variable of Type Stack .

How to Call this Function From Main ?

typedef struct stack
{
  int data[MAX];
  int top;
}stack;

void main()
{
stack s; // Step 1 : Declare Variable of Type Stack
——-
——-
——-
——-
——-
——-
i = empty(&s); // Pass By Reference
——
——
}

  1. Pass “Stack” Variable to “Empty Function” using pass by reference.
  2. As Empty Function returns integer , we have facility to store returned value into some integer variable so we have written [ i = empty(&s) ]

Empty Function :

int empty(stack *s)
{
  if(s->top == -1) //Stack is Empty
     return(1);
  else
     return(0);
}stack;