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...

Queue Operations in C Using Array

✅ Queue Operations in C Using Array

#include<stdio.h>
#define size 5

int queue[size];
int front = -1, rear = -1;

int isempty()
{
    return (front == -1 || front > rear);
}

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

void enqueue(int value)
{
    if(isfull())
    {
        printf("Queue overflow\n");
    }
    else
    {
        if(front == -1)
            front = 0;

        queue[++rear] = value;
        printf("%d added to queue\n", value);
    }
}

void dequeue()
{
    if(isempty())
    {
        printf("Queue underflow\n");
    }
    else
    {
        printf("%d removed from queue\n", queue[front++]);
    }
}

void peek()
{
    if(isempty())
    {
        printf("Queue is empty\n");
    }
    else
    {
        printf("Front element is %d\n", queue[front]);
    }
}

void display()
{
    if(isempty())
    {
        printf("Queue is empty\n");
    }
    else
    {
        printf("Queue elements:\n");
        for(int i = front; i <= rear; i++)
        {
            printf("%d\n", queue[i]);
        }
    }
}

int main()
{
    int choice, data;

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

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

            case 2:
                dequeue();
                break;

            case 3:
                peek();
                break;

            case 4:
                display();
                break;

            case 5:
                return 0;

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

๐Ÿ“˜ Explanation:

This C program implements queue operations using an array. A queue follows the FIFO (First In First Out) principle.

  • Enqueue: Adds an element at the rear of the queue.
  • Dequeue: Removes an element from the front.
  • Peek: Displays the front element without removing it.
  • Display: Shows all elements from front to rear.
  • Overflow: Occurs when queue is full.
  • Underflow: Occurs when queue is empty.

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

๐Ÿงพ Sample Output:

1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter choice: 1
Enter data: 10
10 added to queue

Enter choice: 1
Enter data: 20
20 added to queue

Enter choice: 4
Queue elements:
10
20
  

๐Ÿ”‘ Keywords:

queue in C using array, enqueue dequeue peek display, FIFO queue implementation, data structures C, queue program C

๐Ÿ“Œ Hashtags:

#CProgramming #QueueInC #DataStructures #EnqueueDequeue #CodingBasics #1printf

Comments

Popular Posts

๐ŸŒ™