🌳
B.TECH I YEAR β€’ II SEMESTER β€’ R25

Data Structures - Lab Manual

Data Structures Laboratory β€’ C Programming Solution

πŸ“˜ R25 Regulation πŸŽ“ I Year πŸ“š II Semester πŸ’» C Programming πŸ§ͺ Lab Program
Selected Program

C Program that implement Stack (its operations) using Array

← All Lab Programs

C Program that implement Stack (its operations) using Array

Stack using Array
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_SIZE 5

int stack[MAX_SIZE],top=-1;

// Function to check if the stack is empty
bool isEmpty() {
    return top == -1;
}

// Function to add an item to the stack
void push(int item) {
    if (top == MAX_SIZE - 1) {
        printf("Stack Overflow\n");
    } else {
        stack[++top] = item;
    }
}

// Function to remove an item from the stack
int pop() {
    if (isEmpty()) {
        printf("Stack Underflow\n");
        return -1; // Indicating underflow
    } else {
        return stack[top--];
    }
}

// Function to get the top item of the stack
int peek() {
    if (isEmpty()) {
        printf("Stack is Empty\n");
        return -1; // Indicating empty stack
    } else {
        return stack[top];
    }
}

// Function to show all the items from stack
void show()
{
    int i;
    if (isEmpty()) 
        printf("Stack is Empty\n");
    else{
    for(i=top;i>-1;i--)
        printf("%d\n",stack[i]);
    }
}

// Main function
int main() {
    int ch,data;
    do{
        printf("\n1. Push\n2. Pop\n3. Peek\n4. Show\n5. Exit");
        printf("\nEnter your choice:   ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("Enter data to push:  ");
                    scanf("%d",&data);
                    push(data);
                    break;
            case 2: printf("Popped: %d\n", pop());
                    break;
            case 3: printf("Top element: %d\n", peek());
                    break;
            case 4: show();
                    break;
            case 5: break;
            default: printf("Enter valid choice");
        }
    }while(ch!=5);
    return 0;
}

OUTPUT
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  10
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  20
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  30
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  40
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   3
Top element: 40

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   4
40
30
20
10

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  50
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   1
Enter data to push:  60
Stack Overflow

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   4
50
40
30
20
10

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Popped: 50

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Popped: 40

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Popped: 30

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Popped: 20

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Popped: 10

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   2
Stack Underflow
Popped: -1

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   4
Stack is Empty

1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice:   5

πŸ’» Run This C Program

C Program that implement Stack (its operations) using Array
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
← Back to Data Structures Lab Programs πŸ“š All Lab Manuals