0% found this document useful (0 votes)
36 views4 pages

Dsa Lab

This document describes a C program that implements a stack data structure using a fixed-size array with a maximum capacity of 100 elements. It includes functions for pushing, popping, peeking, checking if the stack is empty, and displaying the stack contents. The main function demonstrates these operations by pushing three elements onto the stack, displaying them, and then popping the top element.

Uploaded by

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

Dsa Lab

This document describes a C program that implements a stack data structure using a fixed-size array with a maximum capacity of 100 elements. It includes functions for pushing, popping, peeking, checking if the stack is empty, and displaying the stack contents. The main function demonstrates these operations by pushing three elements onto the stack, displaying them, and then popping the top element.

Uploaded by

Raju Bhai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

explain the stack implementation in C using an array.

This C program implements a stack (LIFO - Last In First Out) data structure using a fixed-size array. It
allows:

 Pushing (inserting) an element

 Popping (removing) an element

 Peeking (checking top element)

 Displaying stack contents

📁 HEADER AND DEFINITIONS

#include <stdio.h>

#define MAX 100

 #include <stdio.h>: Includes standard I/O functions (printf, scanf, etc.).

 #define MAX 100: Defines the maximum capacity of the stack (100 elements).

STACK ARRAY AND TOP VARIABLE

int stack[MAX];

int top = -1;

 stack[MAX]: Declares a global array to store stack elements.

 top: Keeps track of the top element index. top = -1 means the stack is empty.

FUNCTION: push(int x)

void push(int x) {

if (top >= MAX - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = x;

printf("%d pushed to stack\n", x);

🔹 What it does:

 Adds an element x to the top of the stack.

 First, it checks if the stack is full (top == MAX - 1), to avoid overflow.

 If not full, it increases the top by 1 (++top) and stores x at stack[top].


FUNCTION: pop()

int pop() {

if (top < 0) {

printf("Stack Underflow\n");

return -1;

return stack[top--];

🔹 What it does:

 Removes and returns the top element.

 If the stack is empty (top < 0), it prints an underflow warning.

 Otherwise, it returns the top value and decreases top.

FUNCTION: peek()

int peek() {

if (top < 0) {

printf("Stack is Empty\n");

return -1;

return stack[top];

🔹 What it does:

 Returns the top element without removing it.

 Checks if the stack is empty before accessing.

FUNCTION: isEmpty()

int isEmpty() {

return (top == -1);

🔹 What it does:

 Returns 1 (true) if the stack is empty, 0 otherwise.

FUNCTION: display()
void display() {

if (top < 0) {

printf("Stack is Empty\n");

return;

printf("Stack: ");

for (int i = 0; i <= top; i++)

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

printf("\n");

🔹 What it does:

 Prints all elements in the stack from bottom to top.

 If empty, it says so.

🏁 MAIN FUNCTION

int main() {

push(10);

push(20);

push(30);

display();

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

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

return 0;

🔹 What it does:

 Pushes 10, 20, 30 to the stack.

 Displays the stack: 10 20 30

 Pops the top (30) and prints it.

 Shows the current top element (20).

OUTPUT OF THE PROGRAM:

10 pushed to stack

20 pushed to stack
30 pushed to stack

Stack: 10 20 30

Popped: 30

Top element: 20

You might also like