Data Structures use stack operation to store elements in a disorderly manner

Stack operations in data structure:

A stack is a linear data structure in which the insertion of a new data element and deletion of an existing data element is done from only one end called the Top of the Stack, the last inserted item will be deleted first which is why a stack is also called LIFO (Last in First out) or FIFO (First in First out).

The Basic Stack operations associated with a stack:
1. fnInitialize(S): Initialize the stack S.

2. fnPush(S, iData): Insert the element iData on top of the stack S.

3. fnPop(S): Delete the topmost item from the stack S and return the removed item.

4. fnfull(S): This function checks whether the stack S is full or not. To indicate S is full this function returns TRUE and otherwise FALSE. During the PUSH operation, the Full(S) condition should be checked if there is any restriction on the maximum number of elements in the stack.

5. fnEmpty(S): It is also a Boolean function and returns TRUE for an empty stack and FALSE for a non-empty stack. During POP operation Empty(S) condition is checked.

Implementation of Stack Using Array:

Algorithm for PUSH operation on the stack:

Algorithm fnPush(arrSTACK[], iData)
{
if(fnFull()==TRUE)
else
{
top=top+1;
}
arrSTACK[top]=iData;
}
} //End of Algorithm

Algorithm for POP operation from the stack:

Algorithm fnPop(arrSTACK[])
{
if(fnEmpty()==TRUE)
stack is empty;
else
{
iData=arrSTACK[top];
top=top-1;
return iData;
}
} //End of Algorithm

Leave a Reply

Your email address will not be published. Required fields are marked *