Skip to main content

Featured

Solve LeetCode 30 Substring with Concatenation of All Words in C++ using sliding window and frequency maps

Substring with Concatenation of All Words in C++ LeetCode 30 · sliding window · frequency maps · string_view Table of Contents Problem Statement Approach 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 Conclusion Problem Statement 30. Substring with Concatenation of All Words Given a string s and an array of strings words , where every word has the same length, find all starting indices of substrings that are a concatenation of every word exactly once, in any order. Approach This solution uses a sliding window and two frequency maps. Since every word has the same length, the string is checked in chunks of len characters. freq stores the required frequency of each word. temp stores the frequencies in the current window. count tracks the number of word-sized chunks in the window. If a word occurs too ...

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

๐ŸŒ™