7. Write a c language program to dynamically allocate memory for a queue data structure and implement enqueue and dequeue operations.
To extend a program in C that allocates memory dynamically for a queue data structure and implements enqueue and dequeue operations, we will cover each thing step by step. This C program will exhibit the important actions of a queue by the use of dynamic memory allocation explaining the reason, real life applications and provide specified code explanation.
Purpose
1.Understanding Queues
Implementing a queue information data structure using dynamic memory allocation to grasp the standards of queues in computer technology especially FIFO (First In First Out) data structure systems.
2.Dynamic Memory Allocation
Learning a way to dynamically allocate memory for data structure systems to address variable storage requirement at runtime.
Real-Life Applications
Following are some of the real life applications queues.
Task Scheduling:
Operating structures use queues to achieve which processes are waiting to run. In phrases of the concept of laptop or computer systems queue scheduling is one of the principal tools that determine the execution of threads or processes. These queues are used to consult more than one operations and on the way to differentiate among the execution of those operations.
Resource Management:
Print queue control files are ready to print. Automatically control and manage print jobs and print queues from a key management client. Set rules for disaster recovery, print broadcasting and load balancing. Remote control all direct print traffic throughout the company!
Data Buffering:
Network applications use queues to buffer data packets while waiting for transmission.
The first task is considered first and executed when the next task is completed. Queues are used as buffers to store incoming data before processing. For example, a queue can be used to hold incoming packets in a network before sending them to a lower destination.
a c language program to dynamically allocate memory for a queue and perform dequeue and enqueue operations.
#include<stdio.h>
#include<stdlib.h>
// Structure for a queue
struct Queue {
int front, rear, size;
unsigned capacity;
int *array;
};
// Function to create a queue of given capacity
struct Queue *createQueue(unsigned capacity) {
struct Queue *queue = (struct Queue *)malloc(sizeof(struct Queue));
if (queue == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1; // Important for circular queue implementation
queue->array = (int *)malloc(queue->capacity * sizeof(int));
if (queue->array == NULL) {
printf("Memory allocation failed.\n");
free(queue);
exit(1);
}
return queue;
}
// Function to check if the queue is full
int isFull(struct Queue *queue) {
return (queue->size == queue->capacity);
}
// Function to check if the queue is empty
int isEmpty(struct Queue *queue) {
return (queue->size == 0);
}
// Function to add an element to the queue (enqueue operation)
void enqueue(struct Queue *queue, int item) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size++;
printf("%d enqueued to the queue.\n", item);
}
// Function to remove an element from the queue (dequeue operation)
int dequeue(struct Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1; // Return a sentinel value for error
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size--;
return item;
}
// Function to display the queue contents
void displayQueue(struct Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
int i;
for (i = queue->front; i <= queue->rear; i = (i + 1) % queue->capacity) {
printf("%d ", queue->array[i]);
}
printf("\n");
}
// Main function to demonstrate queue operations
int main() {
struct Queue *queue = NULL;
int choice, item, capacity;
printf("Enter the capacity of the queue: ");
scanf("%d", &capacity);
// Create a queue of given capacity
queue = createQueue(capacity);
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to enqueue: ");
scanf("%d", &item);
enqueue(queue, item);
break;
case 2:
item = dequeue(queue);
if (item != -1) {
printf("Dequeued element: %d\n", item);
}
break;
case 3:
displayQueue(queue);
break;
case 4:
printf("Exiting program.\n");
free(queue->array); // Free allocated memory for queue array
free(queue); // Free allocated memory for queue structure
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Summary
This software program code successfully demonstrates how to implement and manipulate a queue the use of dynamic memory allocation in C. It handles person or user input to perform dequeue, enqueue and display operations while checking for queue complete and empty conditions. The program also ensures right memory control through releasing allotted memory before exiting. The distinctive explanation above must offer a clean know-how of the way this program works and what outputs to be expect based on specific user inputs.
Previous :-->>
6. Write a program in c that dynamically allocates memory for a binary search tree and implements functions to insert and search for elements.
-->> NEXT:
8. Write a C program that dynamically allocates memory for a graph data structure and implements functions to add vertices and edges.
-->>ALL
Dynamic Memory Allocation assignments in c