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

Comments

Popular posts from this blog

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

Agricultural Research Service

National
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…

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

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…

HSC Exam Time Table 2013 Arts, Commerce, Science

HSC Exam Time Table 2013:- Date: 21 Feb 2013 (Thursday)

Paper: (Marathi / Gujarati / Kannada / Sindi / Malyalam / Tamil / Telugu / Punjabi / Bengali
Time: 11.00 am to 2.00 pm

Date: 22 Feb 2013 (Friday)

Paper: Hindi
Time: 11.00 am to 2.00 pm
Paper: German / Andhramagadhi / Persian
Time: 3.00 am to 6.00 pm

Date: 23 Feb 2013 (Saturday)

Paper: English
Time: 11.00 am to 2.00 pm

Date: 25 Feb 2013 (Monday)

Paper: Secretarial Practice
Time: 11.00 am to 2.00 pm
Paper: Physics
Time: 11.00 am to 2.00 pm
Paper: Political Science
Time: 3.00 am to 6.00 pm

Date: 27 Feb 2013 (Wednesday)

Paper: Book Keeping & Accoutancy
Time: 11.00 am to 2.00 pm
Paper: Chemistry (will Be held on 26 March)
Time: 11.00 am to 2.00 pm
Paper: Philosophy
Time: 3.00 am to 6.00 pm

Date: 1 March 2013 (Friday)

Paper: Mathematics & Statistics Paper (Revised)
Time: 11.00 am to 2.00 pm
Paper: Mathematics & Statistics - I (Commerce)
Time: 11.00 am to 1.00 pm
Paper: Sociology
Time: 3.00 am to 6.00 pm

Date: 2 March 2013 (Saturd…