List - Array and Pointer Implementation 15
List - Array and Pointer Implementation 15
Traversal
{
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
struct Node {
int data; // allocate 3 nodes in the heap
struct Node* next; head = (struct Node*)malloc(sizeof(struct Node));
}; second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
// This function prints contents
// of linked list starting from head->data = 1; // assign data in first node
// the given node head->next = second; // Link first node with second
void printList(struct Node* n)
{
second->data = 2; // assign data to second node
second->next = third;
while (n != NULL) {
printf(" %d ", n->data);
third->data = 3; // assign data to third node
n = n->next;
third->next = NULL;
}
} printList(head);
return 0;
}
Applications of linked list