Skip to main content

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


Popular posts from this blog


Agricultural Research Service

Eligibility Test/ Senior Research Fellowship Examination 

The Agricultural Scientists Recruitment Board (ASRB)
 holds a Competitive Examination for recruiting Scientists of the ARS in the pay scale of Rs. 8,000-13,500 in the ICAR Institutes, combined with National Eligibility Test (NET) for recruitment of Lecturers and Assistant Professors by the State Agricultural Universities (SAUS) and for award of ICAR Senior Research Fellowships.

The selected candidates for Agricultural Research Service must serve in the institutes to which they are posted until they find appointment for higher positions through selection at other institutes.

(i) Candidates successful in ARS are appointed as Scientists in the Indian Council of Agricultural Research in the pay scale of Rs. 8,000-13,500.

(ii) Candidates clearing the National Eligibility Test are recommended to various State Agricultural Universities who will consider them for appointment as Lecturers or Assistant Pro…


The Union Public Service Commission (U.P.S.C.) conducts Civil Services' Examination once a year in two stages. The Preliminary Examination (Objective Type) for selection of candidates for the Main Examination is held in the month of May.

The Civil Services Main Examination
 is held in the months of October/November. Blank application forms and other particulars are published in the Employment News, generally in the month of December.

The last date for the submission of applications to the Secretary, Union Public Service Commission, Dholpur House, Shahjahan Road, NewDelhi-11001 1 is usually the last week of January of the year of examination.

The Combined Civil Services Examination is conducted for Recruitment to the following Services/Posts:

1. Indian Administrative Service.
2. Indian Foreign Service.
3. Indian Police Service.
4. Indian P & T Accounts & Finance Service, Group 'A'.
5. Indian Audit and Accounts Service, Group 'A'.
6. Indian Customs and Central Excise S…

Aptitude Test Practice Questions - With Answers

Question 1. Which of the following is least like the others?
A.    cube
B.    sphere
C.    pyramid
D.    circle

D (because the circle is the only two-dimensional figure)

Question 2. Consider a language which uses the following set of characters:
Small set: { a b c }
Large set: { A B C }
Punctuation set: { x y }
This language must follow the following rules:
  1.    A punctuation character must end all series.
  2.    A series can have up to but no more than 4          characters,including punctuation characters.

Does the following series follow all the rules of the language defined above?
  A.    Yes
  B.    No

A (the series has only four characters and ends in a punctuation character)

Question 3. Consider the following flow chart for a customer:

The person in No.1 is:
    A.   Married, with children
    B.   Married, with at least one son
    C.   Unmarried, with at least one daughter
    D.   Unmarried, with at least one son
    E.   Unmarried, with no children