stack_using_linked_list_
stack_using_linked_list_
h>
#include <stdlib.h>
// Define the structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Global pointer to the top of the stack
struct Node* top = NULL;
void push(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Stack is full. Cannot push element.\n");
return;
}
newNode->data = data;
newNode->next = top;
top = newNode;
printf("Pushed element: %d\n", data);
}
switch (choice) {
case 1:
printf("Enter data to push: ");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
if (isEmpty()) {
printf("Stack is empty.\n");
} else {
printf("Stack is not empty.\n");
}
break;
case 5:
if (isFull()) {
printf("Stack is full.\n");
} else {
printf("Stack is not full.\n"); }
break;
case 6:
display();
break;
case 7:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
return 0;
}