4.Write a C program that dynamically allocates memory for a linked list and implements a function to reverse the list.
Objective
Dynamically allocating memory for a linked list in C and executing a function to reverse the list permits you to resourcefully manage and manipulate a collection of data elements in a flexible and dynamic way. By dynamically allocating memory we can create a linked list structure that can grow or shrink as needed during a time of program execution. Reversing a linked list lets permit us to change the order of the elements which can be useful in many circumstances.
Purpose
Efficient memory management
By allocating memory for dynamically linked list we can optimize memory usage by allocating memory only when needed. This helps to reduce memory wastage and improve the overall performance of the program.
Flexibility in data structure
Linked lists offers a flexible data structure where new elements can be easily added removed or rearranged without the need to change other elements. Reversing the list adds another element of flexibility to the data structure.
Real-Life Applications
Social Media Feeds
Dynamically allocated memory in social media platforms can be used for linked lists to receive a feed of posts or updates. Reversing the list allows users to view the feed in sequential or reverse order.
Task Management Applications
Task management applications can use linked lists to create tasks and dynamically allocate memory for new tasks. Reversing the list can help users to sort tasks based on different parameters.
File Management Systems
File management systems can implement linked lists to represent the structure of directories and files. Reversing the list may change the order in which files are displayed or retrieved.
Online Shopping Carts
Online shopping carts can use linked lists to store items added by users. Dynamically allocating memory for new items and reversing the list can help optimize the checkout process.
a C program that dynamically allocates memory for a linked list and implements a function to reverse the list.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list
struct Node
{
int data;
struct Node* next;
};
// Function to add a new node to the linked list
void addNode(struct Node** head, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// Function to reverse the linked list
void reverseList(struct Node** head)
{
struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
// Function to print the linked list
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
struct Node* head = NULL;
// Add nodes to the linked list
addNode(&head, 1);
addNode(&head, 2);
addNode(&head, 3);
addNode(&head, 4);
printf("Original list: ");
printList(head);
// Reverse the linked list
reverseList(&head);
printf("Reversed list: ");
printList(head);
return 0;
}
Program Explanation
Including Libraries
The program begins by including two standard libraries files stdio.h and stdlib.h which offer functions for input output operations and memory allocation.
Defining Node Structure
The program defines a structure named Node that signifies a only single element in the linked list. Separately node contains an integer data field and a pointer to the next node in the list.
Adding a Node to the List
The addNode function is used to add a new node to a linked list. It dynamically allocates memory for the new node assigns the supplied data to it and updates the pointer to maintain list consistency.
Reversing the List
The reverseList function reverses the order of the nodes in the linked list. Continuing the list change the pointers to change the direction of communication between nodes.
Wrinting the List
The printList function is accountable for displaying the elements of a linked list in a specific order. The list head moves from node to node printing each data entry.
Main Function
The main functions task serves as an entry point into the program. It initializes the linked list with NULL and adds four nodes to the list with data values 1, 2, 3, 4. It then prints the original list reverses it using the reverseList function and prints the reverse list back.
Previous :-->> 3. Write a C language program that dynamically allocates memory for a 2D array and calculates the transpose of the matrix.
-->> NEXT:
5. Write a program in C language that dynamically allocates memory for a stack data structure and performs push and pop operations
-->>ALL
Dynamic Memory Allocation assignments in c