A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It can be implemented using a linked list, where each element of the stack is represented as a node. The head of the linked list acts as the top of the stack.
Declaration of Stack using Linked List
A stack can be implemented using a linked list where we maintain:
A Node structure/class that contains: data β to store the element. next β pointer/reference to the next node in the stack.
A pointer/reference top that always points to the current top node of the stack. Initially, top = null to represent an empty stack.