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

Q1

The document contains a C program that implements a stack data structure with a maximum size of 5. It includes functions to check if the stack is full or empty, push and pop elements, peek at the top element, and display the stack contents. The main function demonstrates these operations, including handling stack overflow and underflow scenarios.

Uploaded by

Aryan Kumar
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)
10 views3 pages

Q1

The document contains a C program that implements a stack data structure with a maximum size of 5. It includes functions to check if the stack is full or empty, push and pop elements, peek at the top element, and display the stack contents. The main function demonstrates these operations, including handling stack overflow and underflow scenarios.

Uploaded by

Aryan Kumar
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/ 3

#include <stdio.

h>

#include <stdlib.h>

#define MAX 5 // Defining the maximum size of the stack

// Global variables to represent the stack

int stack[MAX]; // Array to store stack elements

int top = -1; // Top of the stack, initialized to -1 (empty stack)

// Function to check if the stack is full

int isFull() {

if (top == MAX - 1)

return 1; // Stack is full

return 0;

// Function to check if the stack is empty

int isEmpty() {

if (top == -1)

return 1; // Stack is empty

return 0;

// Function to push an element onto the stack

void push(int value) {

if (isFull()) {

printf("Stack Overflow! Cannot push %d\n", value);

} else {top=top+1;

stack[top] = value; // Increment top and add value to stack

printf("Pushed %d onto the stack\n", value);

// Function to pop an element from the stack

int pop() {
if (isEmpty()) {

printf("Stack Underflow! Cannot pop element\n");

return -1; // Return -1 if the stack is empty

} else {

int poppedValue = stack[top]; // Retrieve and remove the top element

top=top-1;

printf("Popped %d from the stack\n", poppedValue);

return poppedValue;

// Function to peek the top element of the stack

int peek() {

if (isEmpty()) {

printf("Stack is empty! Cannot peek\n");

return -1;

} else {

return stack[top]; // Return the top element without removing it

// Function to display the stack

void display() {

if (isEmpty()) {

printf("Stack is empty!\n");

} else {

printf("Stack elements are: ");

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

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

printf("\n");
}

int main() {

// Push elements onto the stack

push(10);

push(20);

push(30);

push(40);

push(50);

// Try pushing another element when the stack is full

push(60); // This should cause stack overflow

// Display the stack

display();

// Pop elements from the stack

pop();

pop();

// Display the stack after popping

display();

// Peek the top element

int topElement = peek();

if (topElement != -1) {

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

return 0;

You might also like