C PROGRAM
Create a singly linked list of N nodes and display it |Data Structure in C|
Introduction
Linked lists offer a dynamic and efficient way to manage data in programming. This C program demonstrates the creation of a linked list using dynamic memory allocation. We'll explore the code, understand the concepts behind dynamic memory allocation for linked lists, and discuss the advantages of using this approach.
Code
#include<stdio.h>
#include<alloc.h>
void main() {
struct node {
int data;
struct node* next;
} * start = NULL;
struct node *newnode, *current;
char ch;
clrscr();
// Creating Linked List
do {
newnode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter the node data: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
if (start == NULL) {
start = newnode;
current = newnode;
} else {
current->next = newnode;
current = newnode;
}
printf("Add more (y/n)? ");
ch = getch();
} while ((ch == 'y') || (ch == 'Y'));
// Displaying Linked List
printf("\nThe linked list is\n");
for (current = start; current != NULL; current = current->next)
printf("%d->", current->data);
printf("NULL");
getch();
}
Output
Enter the node data: 5
Add more (y/n)? y
Enter the node data: 8
Add more (y/n)? y
Enter the node data: 3
Add more (y/n)? n
The linked list is
5->8->3->NULL
In this example, the user inputs three elements (5, 8, and 3) to be added to the linked list. After indicating that no more elements are to be added (n), the program displays the linked list by traversing through each node.
You can try running the program with different sets of elements to observe how the linked list dynamically grows during runtime. The output will always show the elements in the order they were added to the list.
You can try running the program with different sets of elements to observe how the linked list dynamically grows during runtime. The output will always show the elements in the order they were added to the list.
Explanation
1. Header Files:
The program includes the standard input/output header <stdio.h> and the memory allocation header <alloc.h>.
2 Structure Definition:
The node structure represents each element in the linked list. It contains an integer data and a pointer next to the next node.
3. Main Function:
The main function serves as the entry point of the program.
4. Linked List Creation:
Users input elements to be added to the linked list.
Memory is dynamically allocated for each node using malloc.
The linked list is created, and each new node is linked to the previous one.
5. Displaying Linked List:
The program displays the created linked list by traversing through each node.
Advantages of Dynamic Memory Allocation for Linked Lists
1. Flexibility: Dynamic memory allocation allows the linked list to grow or shrink as needed during runtime.
2. Efficiency: Memory is allocated only when required, optimizing memory usage.
3. No Fixed Size Limitation: Unlike arrays, linked lists don't have a fixed size, making them suitable for situations where the number of elements is unknown or can change.
Conclusion
This C program provides a practical example of creating a linked list using dynamic memory allocation. Understanding this approach is crucial for developing scalable and flexible data structures in programming.
Feel free to run the program with different sets of elements to observe how a linked list is dynamically created and displayed. This example serves as a valuable introduction to the dynamic nature of linked lists. Happy coding!
Post a Comment
0 Comments