Untitled
unknown
plain_text
3 years ago
928 B
8
Indexable
#include <stdio.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
int is_empty() {
return top == -1;
}
int is_full() {
return top == MAX_SIZE - 1;
}
void push(int item) {
if (is_full()) {
printf("Stack overflow\n");
return;
}
stack[++top] = item;
}
int pop() {
if (is_empty()) {
printf("Stack underflow\n");
return -1;
}
return stack[top--];
}
int peek() {
if (is_empty()) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
int size() {
return top + 1;
}
int main() {
push(10);
push(20);
push(30);
printf("Peek: %d\n", peek()); // Output: 30
printf("Pop: %d\n", pop()); // Output: 30
printf("Pop: %d\n", pop()); // Output: 20
printf("Size: %d\n", size()); // Output: 1
printf("Is empty? %s\n", is_empty() ? "Yes" : "No"); // Output: No
return 0;
}
Editor is loading...