Bài toán
Cho danh sách liên kết và mảng chỉ số, in dữ liệu các nút tại những chỉ số đó (0-based).
Ví dụ
List: 29→34→43→56→88 Indexes: 1, 2, 4 Output: 34 43 88

Phương pháp
- Duyệt list với con trỏ và biến đếm index
- Khi index khớp với key → in data
- Tiếp tục duyệt đến hết
Mã nguồn C
#include <stdio.h>
#include <stdlib.h>
struct node { int data; struct node *next; };
struct node* intoList(int data) {
struct node* n = malloc(sizeof(struct node));
n->data = data; n->next = NULL; return n;
}
void displayList(struct node* head) {
if (!head) { puts("Rỗng"); return; }
while (head) { printf("%d ", head->data); head = head->next; }
puts("");
}
int search(int key, struct node* head) {
int idx = 0;
while (head && head->data != key) { idx++; head = head->next; }
return head ? idx : -1;
}
int main() {
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);
int idx = search(24, head);
if (idx >= 0) printf("24 tại vị trí %d\n", idx);
else puts("Không tìm thấy 24");
return 0;
}
Kết quả
9 76 13 24 55 109 24 tại vị trí 3