0% found this document useful (0 votes)
2 views1 page

Stack

The document contains a C program that implements a stack data structure using an array. It includes functions for pushing, popping, peeking, checking if the stack is empty, and getting the size of the stack. The main function demonstrates the usage of these stack operations.

Uploaded by

stakehisham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Stack

The document contains a C program that implements a stack data structure using an array. It includes functions for pushing, popping, peeking, checking if the stack is empty, and getting the size of the stack. The main function demonstrates the usage of these stack operations.

Uploaded by

stakehisham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdbool.h>
#include <limits.h>

#define MAX 100

int st[MAX];
int top = -1; // -1 means empty

bool push(int x) {
if (top == MAX - 1) return false; // overflow
st[++top] = x;
return true;
}

int pop(void) {
if (top == -1) return INT_MIN; // underflow
return st[top--];
}

int peek(void) {
if (top == -1) return INT_MIN; // underflow
return st[top];
}

bool isEmpty(void) { return top == -1; }


int size(void) { return top + 1; }

int main(void) {
push(10); push(20);
printf("%d\n", peek()); // 20
printf("%d\n", pop()); // 20
printf("%d\n", pop()); // 10
printf("%d\n", pop()); // INT_MIN (empty)
}

You might also like