How Stack Operates in LIFO fashion, implement Stack Program in C/C++


Stack can be defined as Variant of a list. In stacks, both insertion and deletion are done from one common end. This common end is termed as TOP. The reason why it is called TOP is that a Stack is always visualized vertically but not horizontally, unlike LikedLists. So, elements are added from the top and the very first element comes to the bottom most position. Whereas, the very last element remains on the top. So, when it comes to deletion, the top most element is removed first. That's why it operates in LIFO fashion. Let us go to each point separately below.

How Stacks operates in LIFO fashion?

As we said, assume or visualize Stack vertically, same like vertical ladder having different steps. For understanding purpose, below is a step by step mechanism of LIFO.


Step 1: Assume an empty Stack which is vertically aligned and only opened from one end which is TOP as shown below:



Step 2: Now, we will insert one element, say 17. In stacks inserting elements is called PUSH & deleting any element is termed POP. So, insertion & deletion is called PUCH & POP. So, now, let push one element in above stack which is 17. On pushing 17, it will go to the bottom most position a shown below:


Step 3: Now, again PUSH 5 this time. This time 5 will be placed above 17.

Step 4: Similarly, if we keep adding elements to Stack using PUSH statement, they will be added one on the top of another and the every last element would be on the TOP of the list. So, we have added following elements in the stack overall: 17, 5, 123, 25, 12, 83



Step 5: Now, it comes to delete these elements, so, at this point we can easily understand the concept of LIFO. Now, the last element which we inserted was "83". When we want to remove any element from above stack, we can not remove it from the bottom because that end is closed. The only approach we can adopt for removing the element is removing it from the top as it is the only open end available to us.


Hence, for the very first deletion, we have to remove the element "83", which is one the top. Now, you can see here, "83" was inserted in the last and it is deleted very first. So, LAST in FIRST out, which is nothing but LIFO.

For Code in C++ go here


If you fount this useful the share with your friends 
also do comment

Comments

Popular posts from this blog

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

HSC Exam Time Table 2013 Arts, Commerce, Science

CIVIL SERVICES' (I.A.S.) EXAMINATION