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

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

๐ŸŒ™