STACK USING LINKED LISTS

In this blog post, will study how a singly linked list can be used to implement stacks.

Introduction

The other way of implementing stacks is by using Linked lists. Push operation is implemented by inserting element at the beginning of the list. Pop operation is implemented by deleting the node from the beginning (the header/top node).

Standard Operations

Do refer the code available the end of this section to understand the following theory.

  1. Push: Insert an item at the top of the stack. If there is no place then stack is in overflow state. This operation is done by inserting a new node at front of the list.
  2. Pop: Remove the top item from the stack. If there are no elements then the stack is in underflow state. This operation is done by removing the front node.
  3. isEmpty: To check whether stack is empty or not. If top(head) is null then list is empty.
  4. Peek Access the top item of the stack. Return the data of top node.
  5. Display Display the items in the stack. Traverse the list and print data in each node.

Code file

Please open this in your pc or with a compatible app in your mobile.

C++ Implementation for STACK USING LINKED LISTS

That's it from this blog post. If you liked it then do share this blog with your friends or people who wanna get into programming world. Thank You!

Copyright © NStF Blogs 2021