Linked Lists Using C Programming Language
What is a linked list?
Google says, ' an ordered set of data elements, each containing a link to its successor '. Yes, that's true. I assume that you know about arrays and you have an experience with arrays using C programming language. In C, we have to give the size or assign the values to the array when we declare it as shown below.
Above example, We ought to know the number of data elements that we are going to insert into the array. When we declare an array it reserves consecutive memory locations from the RAM. It is a memory wasting (The size is static) . With linked lists, we can allocate memory location from the RAM dynamically when needed. Here we go...
What are the libraries that we need?
- <stdio.h>
- <stdlib.h>
- which includes functions involving memory allocation, process control, conversions and others.
Creating the node structure
Here we have created a data structure for the node ( 'node' is the name of the data structure) . It contains an integer type variable called 'data' to hold the element's data & node type pointer variable called 'next' to hold the reference to the next node..
Creating the 'head' of the linked list
We need a node type pointer variable to keep the address of the first element of the linked list. Otherwise we cant't find out the beginning of our list. Because the memory locations of the linked list are not consecutive. So I created a variable called 'head' & at the very beginning its value is NULL. Then we have to create a node and reserve a memory location and assign its memory address to the variable 'head'. Here I created the 'append' function...
Append Function(to insert an element at the end of linked list)
First we need to create a temporary variable to hold the newly reserved memory location's address. Then, allocate a memory location and assign its address to the variable we created 'temp'. (we use 'malloc' function of the 'stdlib.h' library. The structure of the function is : -
(cast type) malloc (size of the memory location).
... and malloc returns a memory address. So we cast it to node type pointer)
Then we get an user input for the data and assign & prepare the node by setting its 'next' as NULL. Because we haven't linked it yet.
Before we link the new node, we have to check whether there exists a linked list. So, we check using 'if' condition, the 'head' is NULL or not. Because as we know the 'head' variable represents the beginning node's address. So, if 'head' is NULL, the newly created node should be the first element of the linked list. Then we assign the address of new node to the 'head'.
Otherwise, we create a pointer variable 'p' ( likewise we use integer variable 'i' with for loop) & get the address of the last node using a while loop.
#include
ReplyDelete#include
typedef struct node{
int data;
struct node *next;
};
struct node *head = NULL;
void append(){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &temp->data);
temp -> next = NULL;
if(head == NULL){
head = temp;
}else{
struct node* p;
p = head;
while( p->next != NULL){
p = p->next;
}
p->next = temp;
}
}
Most helpfulππ
ReplyDeleteHelpfull ππ
ReplyDeleteHelpfull π
ReplyDeleteVery Informative Blog. Keep It Up !!
ReplyDeleteGreat start! Keep up the good work.
ReplyDelete