Array&LL Implementation of Stack
Array&LL Implementation of Stack
h>
#include <stdlib.h>
#define MAX 10
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}
int main() {
int choice, value;
while (1) {
printf("1. Push\n2. Pop\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void pop()
{
//temp is used to free the head node
struct Node *temp;
if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head->data);
int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
return 0;
}