INTRODUCTION
Stack is a data structure which follows LIFO i.e. Last-In-First-Out method.
The data/element which is stored last in the stack i.e. the element at top will be accessed first.
And both insertion & deletion takes place at the top.
When we implement stack using array :
As we know that we use a head pointer to keep track of the starting of our linked list, So when we are implementing stack using linked list we can simply call the head pointer as top to make it more relatable to stack.
Push (insert element in stack)
top
pointer will be NULL
. Let's suppose we have to insert the values 1, 2 & 3 in the stack.new
operator and return its address in temporary pointer ptr
.ptr->data = value
and make link part of the node equal to top : ptr->link=top
.top = ptr
to point it to the newly created node which will now be the starting of the linked list and top of our stack.Pop (delete element from stack)
ptr
and equate it to the top
pointer.top = top->link
delete
operator and pointer ptr
i.e delete(ptr)
isEmpty (check if stack is empty or not)
Comments