লিংকড লিস্টের দেওয়া ইনডেক্সগুলোর নোডের ডেটা প্রিন্ট করতে হবে। অ্যারের মত লিংকড লিস্টে সাধারণত ইনডেক্স নেই, তাই পুরো লিস্ট ট্রাভার্স করে নির্দিষ্ট ইনডেক্সে পৌঁছাতে হয়।
উদাহরণ
Linked list: 29->34->43->56->88 Input indexes: 1 2 4 Output: 34 43 88

পীলা হাইলাইট করা নোডগুলোই প্রিন্ট করতে হবে।
অ্যালগরিদম
START
Step 1: Create node structure with int data and node* next
Step 2: Function intoList(int data) to create new node
Step 3: Function displayList(node* head) to print list
Step 4: Function search(int key, node* head) returns index of key
index = 0, newnode = head
While newnode != NULL and newnode->data != key
index++, newnode = newnode->next
Return (newnode != NULL) ? index : -1
Step 5: In main()
Create list, display, search for keys, print index or "not found"
STOP
সি প্রোগ্রাম
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node* intoList(int data) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void displayList(struct node *catchead) {
struct node *temp;
if (catchead == NULL) {
printf("List is empty.\n");
return;
}
printf("elements of list are: ");
temp = catchead;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int search(int key, struct node *head) {
int index = 0;
struct node *newnode = head;
while (newnode != NULL && newnode->data != key) {
index++;
newnode = newnode->next;
}
return (newnode != NULL) ? index : -1;
}
int main() {
int index;
struct node* head = intoList(9);
head->next = intoList(76);
head->next->next = intoList(13);
head->next->next->next = intoList(24);
head->next->next->next->next = intoList(55);
head->next->next->next->next->next = intoList(109);
displayList(head);
index = search(24, head);
if (index >= 0) printf("%d found at position %d\n", 24, index);
else printf("%d not found in the list.\n", 24);
index = search(55, head);
if (index >= 0) printf("%d found at position %d\n", 55, index);
else printf("%d not found in the list.\n", 55);
return 0;
}
আউটপুট
elements of list are: 9 76 13 24 55 109 24 found at position 3 55 found at position 4