Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

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

πŸŒ™