Jul 20, 2024
NULL
.struct Node
containing int data
and struct Node *next
.struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head
or Head Node
.NULL
to denote the end.ptr
to the head node.ptr = ptr->next
.ptr
becomes NULL
.struct Node {
int data;
struct Node *next;
};
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head->data = 7;
head->next = second; // Linking first and second
second->data = 11;
second->next = third; // Linking second and third
third->data = 66;
third->next = NULL; // Ending the list
void linkedlistTraversal(struct Node *ptr) {
while (ptr != NULL) {
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void linkedlistTraversal(struct Node *ptr) {
while (ptr != NULL) {
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int main() {
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
struct Node *second = (struct Node *)malloc(sizeof(struct Node));
struct Node *third = (struct Node *)malloc(sizeof(struct Node));
head->data = 7;
head->next = second;
second->data = 11;
second->next = third;
third->data = 66;
third->next = NULL;
linkedlistTraversal(head);
return 0;
}
NULL
: Marks the end of the linked list.