Bird
0
0
DSA Cprogramming~30 mins

Enqueue Operation in DSA C - Build from Scratch

Choose your learning style9 modes available
Enqueue Operation in a Queue
📖 Scenario: Imagine a line at a ticket counter where people join at the end of the line. This line is called a queue. We will create a queue using an array and add people (numbers) to the end of this queue.
🎯 Goal: You will build a simple queue using an array and write the code to add (enqueue) elements to the queue.
📋 What You'll Learn
Create an integer array called queue with size 5
Create an integer variable called rear initialized to -1
Write a function called enqueue that adds an integer to the queue if there is space
Print the queue elements after adding new elements
💡 Why This Matters
🌍 Real World
Queues are used in real life for lines, like waiting for a bus or at a bank. In computers, queues help manage tasks in order.
💼 Career
Understanding queues is important for software jobs that involve task scheduling, resource management, and data processing.
Progress0 / 4 steps
1
Create the queue array and rear variable
Create an integer array called queue with size 5 and an integer variable called rear initialized to -1.
DSA C
Hint

Use int queue[5]; to create the array and int rear = -1; to start the rear pointer.

2
Create the enqueue function header
Write the function header for void enqueue(int value) that will add a value to the queue.
DSA C
Hint

Start the function with void enqueue(int value) {

3
Implement enqueue logic to add element
Inside the enqueue function, check if rear is less than 4. If yes, increase rear by 1 and add value to queue[rear]. Close the function with a closing brace }.
DSA C
Hint

Use if (rear < 4) to check space, then rear++ and assign queue[rear] = value;.

4
Add elements and print the queue
Call enqueue three times with values 10, 20, and 30. Then use a for loop from 0 to rear to print each element in queue separated by spaces.
DSA C
Hint

Call enqueue(10); enqueue(20); enqueue(30); then print elements with a for loop and printf.