Menu

[R22] Data Structures Lab Manual [ Lab Programs ]


Aim:

Write a program that implement Stack (its operations) using Array

Solution :

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

Related Content :

Data Structures Lab Programs

1) Write a program that uses functions to perform the following operations on singly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

2) Write a program that uses functions to perform the following operations on doubly linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

3) Write a program that uses functions to perform the following operations on circular linkedlist.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
View Solution

4) Write a program that implement Stack (its operations) using Array View Solution

5) Write a program that implement Stack (its operations) using Linked List (Pointer) View Solution

6) Write a program that implement Queue(its operations) using Array View Solution

7) Write a program that implement Queue (its operations) using Linked List (Pointer) View Solution

8) Write a program that implements Quick sort sorting methods to sort a given list of integers in ascending order View Solution

9) Write a program that implements Merge sort sorting methods to sort a given list of integers in ascending order View Solution

10) Write a program that implements Heap sort sorting methods to sort a given list of integers in ascending order View Solution

11) Write a program to implement the tree traversal methods using Recursive View Solution

12) Write a program to implement the tree traversal methods using Non Recursive View Solution

13) Write a program to implement Binary Search Tree (its operations) View Solution

14) Write a program to implement AVL Tree (its operations) View Solution

15) Write a program to implement Red - Black Tree (its operations) View Solution

16) Write a program to implement B Trees (its operations) View Solution

17) Write a program to implement B+ Trees (its operations) View Solution

18) Write a program to implement the graph traversal methods (Breadth First Search) View Solution

19) Write a program to implement the graph traversal methods (Depth First Search) View Solution

20) Write a program to Implement a Pattern matching algorithms using Boyer- Moore View Solution

21) Write a program to Implement a Pattern matching algorithms using Knuth-Morris-Pratt View Solution