লিংকড লিস্ট ডাইনামিক মেমরি অ্যালোকেশন ব্যবহার করে, অর্থাৎ এটি প্রয়োজন অনুযায়ী বড় ও ছোট হয়। এটি নোডের একটি সংগ্রহ হিসাবে সংজ্ঞায়িত। এখানে প্রতিটি নোডের দুটি অংশ থাকে—ডেটা এবং লিংক। ডেটা, লিংক এবং লিংকড লিস্টের রিপ্রেজেন্টেশন নিচে দেওয়া হলো—

লিংকড লিস্টে অপারেশন
C ভাষায় লিংকড লিস্টে মূলত তিনটি ধরণের অপারেশন করা যায়—
- ইনসারশন (Insertion)
- ডিলিশন (Deletion)
- ট্রাভার্সিং (Traversing)
ইনসারশন (Insertion)
একটি উদাহরণ বিবেচনা করুন, যেখানে নোড ৫কে নোড ২ এবং নোড ৩-এর মাঝপথে ইনসার্ট করা হচ্ছে।

এখন নোড ৫কে শুরুতে ইনসার্ট করা হচ্ছে।

নোড ৫কে শেষে ইনসার্ট করা হচ্ছে।

নোড ৫কে আবার শেষে ইনসার্ট করা হচ্ছে (ডুপ্লিকেট ইলাস্ট্রেশন)।

দ্রষ্টব্য:
- নোড ৫কে নোড ২-এর আগে ইনসার্ট করা যাবে না, কারণ নোডগুলো নাম দিয়ে চিহ্নিত নয়।
- যদি পজিশন দেওয়া থাকে, তবে নোড ৫কে নোড ২-এর আগে ইনসার্ট করা সম্ভব।
প্রোগ্রাম
লিংকড লিস্টে এলিমেন্ট ইনসার্ট করার জন্য C প্রোগ্রাম নিচে দেওয়া হলো—
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node *next;
};
void print_list(struct node *head) {
printf("H->");
while (head) {
printf("%d->", head->val);
head = head->next;
}
printf("......\n\n");
}
void insert_front(struct node **head, int value) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf(" Out of memory");
return;
}
new_node->val = value;
new_node->next = *head;
*head = new_node;
}
void insert_end(struct node **head, int value) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
struct node *last = NULL;
if (new_node == NULL) {
printf(" Out of memory");
return;
}
new_node->val = value;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
last = *head;
while (last->next) last = last->next;
last->next = new_node;
}
void insert_after(struct node *head, int value, int after) {
struct node *new_node = NULL;
struct node *tmp = head;
while (tmp) {
if (tmp->val == after) { /* নোডটি পাওয়া গেলে */
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Out of memory");
return;
}
new_node->val = value;
new_node->next = tmp->next;
tmp->next = new_node;
return;
}
tmp = tmp->next;
}
}
void insert_before(struct node **head, int value, int before) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
struct node *tmp = *head;
if (new_node == NULL) {
printf("Out of memory");
return;
}
new_node->val = value;
if ((*head)->val == before) {
new_node->next = *head;
*head = new_node;
return;
}
while (tmp && tmp->next) {
if (tmp->next->val == before) {
new_node->next = tmp->next;
tmp->next = new_node;
return;
}
tmp = tmp->next;
}
/* Before node not found */
free(new_node);
}
int main() {
int count = 0, i, val, after, before;
struct node *head = NULL;
printf("Enter no: of elements: ");
scanf("%d", &count);
for (i = 0; i < count; i++) {
printf("Enter %dth element: ", i);
scanf("%d", &val);
insert_front(&head, val);
}
printf("starting list: ");
print_list(head);
printf("enter front element: ");
scanf("%d", &val);
insert_front(&head, val);
printf("items after insertion: ");
print_list(head);
printf("enter last element: ");
scanf("%d", &val);
insert_end(&head, val);
printf("items after insertion: ");
print_list(head);
printf("Enter an ele to insert in the list: ");
scanf("%d", &val);
printf("Insert after: ");
scanf("%d", &after);
insert_after(head, val, after);
printf("List after insertion: ");
print_list(head);
printf("Enter an ele to insert in the list: ");
scanf("%d", &val);
printf("Insert before: ");
scanf("%d", &before);
insert_before(&head, val, before);
printf("List after insertion: ");
print_list(head);
return 0;
}
আউটপুট
উপরোক্ত প্রোগ্রামটি এক্সিকিউট করলে নিচের ফলাফলটি পাওয়া যায়—
Enter no: of elements: 4 Enter 0th element: 1 Enter 1th element: 2 Enter 2th element: 3 Enter 3th element: 4 starting list: H->4->3->2->1->...... enter front element: 5 items after insertion: H->5->4->3->2->1->...... enter last element: 0 items after insertion: H->5->4->3->2->1->0->...... Enter an ele to insert in the list: 6 Insert after: 0 List after insertion: H->5->4->3->2->1->0->6->...... Enter an ele to insert in the list: 7 Insert before: 5 List after insertion: H->7->5->4->3->2->1->0->6->......