Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print nodes of linked list at given indexes in C language
In C programming, printing nodes of a linked list at given indexes involves traversing the list and accessing elements at specific positions. Unlike arrays, linked lists don't have direct indexing, so we must traverse from the head node counting positions until we reach the desired index.
Syntax
void printAtIndex(struct node* head, int index); void printMultipleIndexes(struct node* head, int indexes[], int size);
Example 1: Print Single Node at Given Index
This example demonstrates how to print a node at a specific index −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printAtIndex(struct node* head, int index) {
struct node* current = head;
int count = 0;
while (current != NULL) {
if (count == index) {
printf("Node at index %d: %d<br>", index, current->data);
return;
}
current = current->next;
count++;
}
printf("Index %d is out of bounds<br>", index);
}
int main() {
struct node* head = createNode(29);
head->next = createNode(34);
head->next->next = createNode(43);
head->next->next->next = createNode(56);
head->next->next->next->next = createNode(88);
printf("Linked list: 29->34->43->56->88<br>");
printAtIndex(head, 1);
printAtIndex(head, 3);
printAtIndex(head, 5);
return 0;
}
Linked list: 29->34->43->56->88 Node at index 1: 34 Node at index 3: 56 Index 5 is out of bounds
Example 2: Print Multiple Nodes at Given Indexes
This approach prints nodes at multiple specified indexes efficiently −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* createNode(int data) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printMultipleIndexes(struct node* head, int indexes[], int size) {
struct node* current = head;
int position = 0;
int i = 0;
printf("Nodes at indexes ");
for (int j = 0; j < size; j++) {
printf("%d ", indexes[j]);
}
printf(": ");
while (current != NULL && i < size) {
if (position == indexes[i]) {
printf("%d ", current->data);
i++;
}
current = current->next;
position++;
}
printf("<br>");
}
int main() {
struct node* head = createNode(29);
head->next = createNode(34);
head->next->next = createNode(43);
head->next->next->next = createNode(56);
head->next->next->next->next = createNode(88);
printf("Linked list: 29->34->43->56->88<br>");
int indexes[] = {1, 2, 4};
int size = sizeof(indexes) / sizeof(indexes[0]);
printMultipleIndexes(head, indexes, size);
return 0;
}
Linked list: 29->34->43->56->88 Nodes at indexes 1 2 4 : 34 43 88
Key Points
- Linked list indexing starts from 0 (first node is at index 0)
- Time complexity is O(n) where n is the index position
- Always check for NULL pointers to avoid segmentation faults
- For multiple indexes, sort them first for optimal traversal
Conclusion
Printing nodes at specific indexes in a linked list requires sequential traversal from the head. The approach is straightforward but less efficient than array indexing due to the linear access pattern of linked lists.
Advertisements
