#include <stdio.h>
#include <stdlib.h>
// Node structure for stack
typedef struct Node {
int data;
struct Node* next;
} Node;
// Push function to add element on top
void push(Node** top_ref, int value) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = value;
new_node->next = *top_ref; // link new node to current top
*top_ref = new_node; // update top to new node
}
// Print stack from top to bottom
void printStack(Node* top) {
Node* current = top;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("null\n");
}
int main() {
Node* stackTop = NULL; // empty stack
push(&stackTop, 10);
push(&stackTop, 20);
push(&stackTop, 30);
printStack(stackTop);
return 0;
}new_node->next = *top_ref; // link new node to current top
link new node to current top to keep stack order
*top_ref = new_node; // update top to new node
update top pointer to new node to make it the new top