Skip to main content

STACK

##
It is a linear data structre that follows the principle of LIFO i.e Last In First Out

Assume you are putting plates on top of previous plate ,like below :

Plate-A -> Plate-B -> Plate-C
       Plate-A ->  Plate-B
          ->  Plate-A

in order to get the Plate-A we need to remove all the plate on top of Plate-A which are Plate-B ,Plate-C
which is the plate added at last will be remove first which is nothing but LIFO.


##
Stack Operations:
1. Push Operation : to add an element in the stack
2. Pop Operation : to remove an element from TOP of the stack
3. Peek Operation : to print the topmost element without removing it from the stack
4. is_full Operation : whether stack is full or not
5. is_empty Operation: whether stack is empty or not

##
Demonstration :
#
we will include the mandate libraries first
Then we will define the struct of stack using struct in c.

#
Although there are n number of ways to implement stack ,so demonstration here is one of the way.
We will create a method name 'create_empty_stack()' where we will initialize the TOP to -1 which means currently stack is empty
Another method named 'is_full()' this method will return 1 if stack is FULL and return 0 if stack is not FULL.
#
Now the third method should be defined is 'is_empty()'
purpose of this method will be to return 1 if our stack is EMPTY means nothing is in stack or will return 0 if stack is not empty.
#
Now this method will be the most important method 'push()' becuase we will add the elements into the stack with help of this method.And if stack is not full then we will push an element into stack else we will do nothing since the return type of method is void
But before pushing any element into the stack we first have to check if stack is full or not with help of above defined methods and if stack is not FULL then we will add the element in stack at TOPth position and increment the TOP.
#
The Last one of the important method we will define is 'pop()' because with help of this method we will remove elements from the stack ,but before removing first we have to verify that stack is not empty with the help of above defined method and if stack is not empty then we will simply decerment the value of TOP ,so for instance if last element in stack at TOPth position added was 5 then we will point to the TOP - 1 position as last pushed value.
Example : considere a simple STACK[1,2 ,3 ,4 ,5] ,last element pushed is 5 at TOPth position so if we decrement the TOP by -1 then our last element pushed will be 4 and the TOP will be pointing to index 3.
#
Optional method 'print_stack()' : this is not important because this method is only to display the elements available in stack

#
Now lets move to the last section of this code lets define the main method and try to push & pop elements in / from the stack
THe very first thing we will do is allocate the memory to stack in heap via malloc method ,then push() the elements in the stack and then pop() and at last print_stack.

Thats all for this section ,you can find the whole code in python as well as in c along with its one of the application in this repository link : stacks