Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Implement Stack Using Linked List

Objec­tive: Write an algo­rithm to imple­ment Stack using Linked List.

If you do not know about then for starters its abstract data type in which fol­lows the prin­ci­ple of LIFO (Last-In-First-Out) which means the data goes in last comes out first to read about in detail please read this link Stack

Approach:

Solu­tion is quite sim­ple, Ear­lier we have seen an arti­cle  “Linked List Imple­men­ta­tion”, we need to make some changes to make it work as Stack.

Stack Oper­a­tions:

Push() : Insert the ele­ment into linked list at the begin­ning and increase the size of the list. O(1) operation.

Pop() : Return the ele­ment first node from the linked list and move the head pointer to the sec­ond node. Decrease the size of the list. O(1) operation.

get­Size(): Return the size of linked list.

dis­playStack(): Print the linked list.

Com­plete Code:

Out­put:

Element 1 is pushed into Stack
Element 2 is pushed into Stack
Element 4 is pushed into Stack
4 2 1
Pop out element 4
2 1

Note: Also read, Track the Max­i­mum Ele­ment in a Stack. This prob­lem was asked in Yahoo in Soft­ware Engi­neer position.

You may also like...