0% found this document useful (0 votes)
3 views3 pages

Stack Using Array

This document contains a C program that implements a stack data structure with basic operations including push, pop, peek, and display. It defines a stack of fixed size and handles overflow and underflow conditions. The main function demonstrates pushing elements onto the stack, popping an element, and displaying the current stack state.

Uploaded by

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

Stack Using Array

This document contains a C program that implements a stack data structure with basic operations including push, pop, peek, and display. It defines a stack of fixed size and handles overflow and underflow conditions. The main function demonstrates pushing elements onto the stack, popping an element, and displaying the current stack state.

Uploaded by

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

#include <stdio.

h>

#define SIZE 5

int stack[SIZE];

int top = -1;

// ---- PUSH ----

void push(int x) {

if (top == SIZE - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = x;

printf("Pushed %d\n", x);

// ---- POP ----

int pop() {

if (top == -1) {

printf("Stack Underflow\n");

return -1;

return stack[top--];

// ---- PEEK ----

int peek() {

if (top == -1) {
printf("Stack Empty\n");

return -1;

return stack[top];

// ---- DISPLAY ----

void display() {

if (top == -1) {

printf("Stack Empty\n");

return;

printf("Stack: ");

for (int i = top; i >= 0; i--)

printf("%d ", stack[i]);

printf("\n");

int main() {

push(10);

push(20);

push(30);

display();

printf("Popped: %d\n", pop());

printf("Top element: %d\n", peek());


display();

return 0;

You might also like