Simple Stack Implementation in C

mail@pastecode.io avatar
unknown
c_cpp
a month ago
654 B
1
Indexable
Never
#include <stdio.h>
#include <conio.h>

int stack[100];
int top;
void display();
void push(int num);
void pop();
int i;

int main()
{
	top = -1;
	push(10);
	push(9);
	push(8);
	push(7);
	display();
	pop();
	display();
	pop();
	display();
	pop();
	display();
	pop();
	display();
	pop();
	display();
	return 0;
}

void pop() {
	if(top == -1) {
		printf("\nUnderflow\n");
	}
	else {
		top--;
	}
}

void push(int num) {
	if(top == 99) {
		printf("\nOverflow\n");
	}
	else {
		top++;
		stack[top] = num;
	}
}

void display() {
	if(top == -1) {
		printf("\nEmpty\n");
	}
	printf("\n");
	for(i=0; i<=top; i++) {
		printf("%d \n",stack[i]);
	}
	printf("\n");
}


Leave a Comment