80 lines
1.6 KiB
C
80 lines
1.6 KiB
C
#include <stdio.h>
|
|
#define MAX_SIZE 100
|
|
|
|
int top = -1;
|
|
int stack[MAX_SIZE];
|
|
|
|
void push(int c) {
|
|
if (top < MAX_SIZE - 1) {
|
|
stack[++top] = c;
|
|
} else {
|
|
printf("Stack overflow\n");
|
|
}
|
|
}
|
|
|
|
int pop() {
|
|
if (top >= 0) {
|
|
return stack[top--];
|
|
} else {
|
|
printf("Stack underflow\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
void display() {
|
|
printf("The stack is: ");
|
|
for (int i = top; i >= 0; i--) {
|
|
printf("%d\t", stack[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int isEmpty() {
|
|
return top == -1;
|
|
}
|
|
|
|
int peek() {
|
|
if (!isEmpty()) {
|
|
return stack[top];
|
|
} else {
|
|
printf("Stack is empty\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
int choice;
|
|
while (1) {
|
|
printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Check if Empty\n6. Exit\n");
|
|
scanf("%d", &choice);
|
|
switch (choice) {
|
|
case 1:
|
|
printf("Enter an element: ");
|
|
int element;
|
|
scanf("%d", &element);
|
|
push(element);
|
|
break;
|
|
case 2:
|
|
printf("The top element is: %d\n", pop());
|
|
break;
|
|
case 3:
|
|
display();
|
|
break;
|
|
case 4:
|
|
printf("The top element is: %d\n", peek());
|
|
break;
|
|
case 5:
|
|
if (isEmpty()) {
|
|
printf("Stack is empty\n");
|
|
} else {
|
|
printf("Stack is not empty\n");
|
|
}
|
|
break;
|
|
case 6:
|
|
return 0;
|
|
default:
|
|
printf("Invalid choice.\n");
|
|
}
|
|
}
|
|
}
|