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 -
- Function returns “True” if Stack is Empty.
- Function returns “False” if Stack is Non-Empty.
- Function Takes “Pointer to Stack”
int empty (stack *s)
- Return Type : Integer. [Empty Stack Return 1 , Non Empty Stack Return 0 ]
- 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
——
——
}
- Pass “Stack” Variable to “Empty Function” using pass by reference.
- 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;