Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™