#include <stdio.
h>
#include <stdlib.h> // For malloc and exit()
struct node {
int label;
struct node *next;
};
int main() {
int ch = 0;
struct node *h, *temp, *head;
// Stack head node (dummy node)
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
while (1) {
printf("\nStack using Linked List\n");
printf("1 -> Push\n");
printf("2 -> Pop\n");
printf("3 -> View\n");
printf("4 -> Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: // Push
temp = (struct node *)malloc(sizeof(struct node));
if (!temp) {
printf("Memory allocation failed\n");
break;
}
printf("Enter label for new node: ");
scanf("%d", &temp->label);
temp->next = head->next;
head->next = temp;
printf("Node pushed successfully.\n");
break;
case 2: // Pop
if (head->next == NULL) {
printf("Stack Underflow\n");
} else {
temp = head->next;
head->next = temp->next;
printf("Node %d deleted\n", temp->label);
free(temp);
}
break;
case 3: // View
printf("\nHEAD -> ");
h = head->next;
while (h != NULL) {
printf("%d -> ", h->label);
h = h->next;
}
printf("NULL\n");
break;
case 4: // Exit
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}