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() { ...

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

πŸŒ™