Updated March 31, 2023
Introduction to Linked List in C
As the name suggests linked list means linking lists together or we can say that a linked list is the sequence of data structures that are connected to each other via links. Linked list use pointer for its implementation in the data structure. It’s a linear data structure in which data is stored at different locations and linked using pointers. Linked list node has two parts one is the data part and the other is the address part which has many advantages in insertion and deletion of the element from a particular position without wasting any time because it saves memory space dynamically by changing size.
Syntax
Let’s have a look at the syntax of representing a linked list in your code:
struct node {
int data ;
struct node *next ;
} ;
In the above-linked list syntax struct is the mandatory keyword to be used because with help of structure we can create custom data structure and as it is a node so node keyword is used so we are creating a data structure and in node, we have two parts, one is integer data part while other is the pointer which we address to the next node in the list.
Basically, the linked list node has two parts:
- A data part: It will contain the data of the user
- A pointer part: It will always point to the next member of the linked list in code.
How Linked List work in C?
Now we will discuss the working of the linked list through C code with a brief explanation. Here is the C code to demonstrate the working of the linked list:
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data ;
struct node *next ;
};
struct node *start = NULL ;
void insert_begin(int) ;
void insert_end(int) ;
void traverse() ;
void delete_begin() ;
void delete_end() ;
int count = 0 ;
int main () {
int x, data ;
for (;;) {
printf("1. Want to insert an element at the beginning of linked list.\n") ;
printf("2. Want to insert an element at the end of linked list.\n") ;
printf("3. Want to traverse linked list.\n") ;
printf("4. Want to delete an element from beginning.\n") ;
printf("5. Want to delete an element from end.\n") ;
printf("6. Exit\n") ;
scanf("%d", &x) ;
if (x == 1) {
printf("Enter value of element\n") ;
scanf("%d", &data) ;
insert_begin(data) ;
}
else if (x == 2) {
printf("Enter value of element\n") ;
scanf("%d", &data) ;
insert_end(data) ;
}
else if (x == 3)
traverse() ;
else if (x == 4)
delete_begin() ;
else if (x == 5)
delete_end() ;
else if (x == 6)
break ;
else
printf("Please enter valid input.\n") ;
}
return 0 ;
}
void insert_begin(int i) {
struct node *t ;
t = (struct node*)malloc(sizeof(struct node)) ;
t -> data = i;
count++ ;
if (start == NULL) {
start = t ;
start->next = NULL ;
return ;
}
t->next = start ;
start = t ;
}
void insert_end(int i) {
struct node *t, *temp ;
t = (struct node*)malloc(sizeof(struct node));
t -> data = i;
count++ ;
if (start == NULL) {
start = t ;
start->next = NULL ;
return ;
}
temp = start ;
while (temp->next != NULL)
temp = temp->next ;
temp->next = t ;
t->next = NULL ;
}
void traverse() {
struct node *t ;
t = start ;
if (t == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
printf("There are %d elements in linked list.\n", count) ;
while (t->next != NULL) {
printf("%d\n", t->data) ;
t = t->next ;
}
printf("%d\n", t->data); // Print last node
}
void delete_begin() {
struct node *t ;
int n ;
if (start == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
n = start->data ;
t = start->next ;
free(start) ;
start = t ;
count-- ;
printf("%d deleted from the beginning successfully.\n", n) ;
}
void delete_end() {
struct node *t, *u ;
int n;
if (start == NULL) {
printf("Linked list is empty.\n") ;
return ;
}
count-- ;
if (start->next == NULL) {
n = start->data ;
free(start) ;
start = NULL ;
printf("%d deleted from end successfully.\n", n) ;
return ;
}
t = start ;
while (t->next != NULL) {
u = t ;
t = t->next ;
}
n = t->data ;
u->next = NULL ;
free(t);
printf( "%d deleted from end successfully.\n ", n) ;
}
Output:
Explanation:
In the above code, we have created a node and we have also created the address part of a node. We have added 5 main functionality of linked that help in performing all kinds of possible operations in our code. So we have declared insertion, deletion operations at the beginning as well as the end of the linked list. One more function is declared for traversing the list linked together.
In the main class, we have declared two variables of integer data type as “I” and “data”. After declaring all the functionalities in beginning we will implement an if-else loop so that we can switch between the mentioned functionalities. For all the 5 functionalities we are implementing the logic in our code through an algorithm.
For insertion in the list at the beginning, we created a node t and in its data part, we named it as x. Therefore, if the start is null then start will be put in node t data part and the address part will point to the next part which is NULL. This process will insert the element at the beginning. In the same way, we have defined the logic for insertion and deletion at the start and end of the linked list in our code.
Conclusion
Memory utilization plays a crucial role in the complex as well as simpler codes. Therefore, with the help of Linked list memory utilization can be done easily and efficiently. Most useful when we don’t have an idea about the size of the list. It can grow and shrink accordingly.
Recommended Articles
This is a guide to Linked List in C. Here we discuss the introduction to Linked List in C, along with the syntax, how Linked List work and an example to implement. You can also go through our other related articles to learn more –