Stack Implementation Using Linked List A stack works on the LIFO (Last In First
Out) principle. When using a linked list, the TOP pointer always refers to the first
node.
TOP
↓
+-------+ +-------+ +-------+
| 40 | ---> | 30 | ---> | 20 |
+-------+ +-------+ +-------+
PUSH Operation Steps: 1. Create a new node. 2. Set newnode->next = TOP. 3.
Update TOP = newnode. After PUSH(50):
TOP
↓
+-------+ +-------+ +-------+ +-------+
| 50 | ---> | 40 | ---> | 30 | ---> | 20 |
+-------+ +-------+ +-------+ +-------+
POP Operation Steps: 1. temp = TOP. 2. TOP = TOP->next. 3. Free temp. After
POP():
TOP
↓
+-------+ +-------+ +-------+
| 40 | ---> | 30 | ---> | 20 |
+-------+ +-------+ +-------+