Method1
Method1
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Define the maximum size of the stack
// Stack variables
int top = -1;
int stack[MAX];
// Function to add an element to the stack
void push(int value) {
if (top == MAX - 1) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
stack[++top] = value;
printf("%d pushed to stack\n", value);
}
}
// Function to remove an element from the stack
void pop() {
if (top == -1) {
printf("Stack underflow! Cannot pop\n");
} else {
printf("%d popped from stack\n", stack[top--]);
}
}
// Function to get the top element of the stack
void peek() {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Top element is %d\n", stack[top]);
}
}
// Function to check if the stack is empty
int isEmpty() {
return top == -1;
}
// Function to check if the stack is full
int isFull() {
return top == MAX - 1;
}
int main() {
int n;
char choice;
// Push elements into the stack
do {
printf("\nEnter item to push into the stack: ");
scanf("%d", &n);
push(n); // Push the entered item onto the stack
printf("Do you want to push another item into the stack (y/n)? ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
// Peek at the top element
peek();
// Pop the top element
pop();
pop();
// Peek at the new top element
peek();
// Check if the stack is empty or full
if (isEmpty()) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
if (isFull()) {
printf("Stack is full\n");
} else {
printf("Stack is not full\n");
}
return 0;
}
Method2
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Define the maximum size of the stack
// Stack structure definition
struct Stack {
int top;
int arr[MAX]; // Array to store stack elements
};
// Initialize the stack
void initialize(struct Stack* stack) {
stack->top = -1; // Initialize top of the stack
}
// Function to add an element to the stack
void push(struct Stack* stack, int value) {
if (stack->top == MAX - 1) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
stack->arr[++(stack->top)] = value;
printf("%d pushed to stack\n", value);
}
}
// Function to remove an element from the stack
void pop(struct Stack* stack) {
if (stack->top == -1) {
printf("Stack underflow! Cannot pop\n");
} else {
printf("%d popped from stack\n", stack->arr[(stack->top)--]);
}
}
// Function to get the top element of the stack
void peek(struct Stack* stack) {
if (stack->top == -1) {
printf("Stack is empty!\n");
} else {
printf("Top element is %d\n", stack->arr[stack->top]);
}
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
int main() {
struct Stack stack; // Create a stack object
initialize(&stack); // Initialize the stack
int n;
char choice;
// Push elements into the stack
do {
printf("\nEnter item to push into the stack: ");
scanf("%d", &n);
push(&stack, n); // Push the entered item onto the stack
printf("Do you want to push another item into the stack (y/n)? ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
// Peek at the top element
peek(&stack);
// Pop the top element
pop(&stack);
pop(&stack);
// Peek at the new top element
peek(&stack);
// Check if the stack is empty or full
if (isEmpty(&stack)) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
if (isFull(&stack)) {
printf("Stack is full\n");
} else {
printf("Stack is not full\n");
}
return 0;
}