Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

Stack Operations in C Using Array

✅ Stack Operations in C Using Array

#include<stdio.h>
#define size 5

int top = -1;
int stack[size];

int isempty()
{
    return (top == -1);
}

int isfull()
{
    return (top == size - 1);
}

void push(int value)
{
    if(isfull())
    {
        printf("Stack overflow\n");
    }
    else
    {
        stack[++top] = value;
        printf("%d added successfully to stack\n", value);
    }
}

void pop()
{
    if(isempty())
    {
        printf("Stack underflow\n");
    }
    else
    {
        printf("%d popped from stack\n", stack[top--]);
    }
}

void peek()
{
    if(isempty())
    {
        printf("Stack is empty\n");
    }
    else
    {
        printf("Top element is %d\n", stack[top]);
    }
}

void display()
{
    if(isempty())
    {
        printf("Stack is empty\n");
    }
    else
    {
        printf("Stack elements are:\n");
        for(int i = top; i >= 0; i--)
        {
            printf("%d\n", stack[i]);
        }
    }
}

int main()
{
    int choice, data;

    while(1)
    {
        printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                printf("Enter data: ");
                scanf("%d", &data);
                push(data);
                break;

            case 2:
                pop();
                break;

            case 3:
                peek();
                break;

            case 4:
                display();
                break;

            case 5:
                return 0;

            default:
                printf("Invalid choice\n");
        }
    }
}
  

๐Ÿ“˜ Explanation:

This C program implements stack operations using an array. A stack follows the LIFO (Last In First Out) principle.

  • Push: Adds an element to the top of the stack.
  • Pop: Removes the top element from the stack.
  • Peek: Displays the top element without removing it.
  • Display: Shows all stack elements from top to bottom.
  • Overflow: Occurs when stack is full.
  • Underflow: Occurs when stack is empty.

This is a basic and important data structure implementation in C.

๐Ÿงพ Sample Output:

1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter data: 10
10 added successfully to stack

Enter your choice: 1
Enter data: 20
20 added successfully to stack

Enter your choice: 4
Stack elements are:
20
10
  

๐Ÿ”‘ Keywords:

stack in C using array, push pop peek display, data structures C, LIFO stack implementation, stack program C

๐Ÿ“Œ Hashtags:

#CProgramming #StackInC #DataStructures #PushPop #CodingBasics #1printf

Comments

Popular Posts

๐ŸŒ™