0% found this document useful (0 votes)
37 views2 pages

C Linked List Node Creation Example

Uploaded by

trw.dcn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

C Linked List Node Creation Example

Uploaded by

trw.dcn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

#include <stdlib.h>

// Define a structure for a node in the linked list

struct Node {

int data; // Data part to store an integer

struct Node* next; // Pointer to the next node

};

int main() {

// Create three nodes

struct Node* head = NULL;

struct Node* second = NULL;

struct Node* third = NULL;

// Allocate memory for three nodes in the linked list

head = (struct Node*)malloc(sizeof(struct Node));

second = (struct Node*)malloc(sizeof(struct Node));

third = (struct Node*)malloc(sizeof(struct Node));

// Assign data and link the nodes

head->data = 1; // First node has data 1

head->next = second; // First node points to second node

second->data = 2; // Second node has data 2

second->next = third; // Second node points to third node

third->data = 3; // Third node has data 3

third->next = NULL; // Third node is the last node, points to NULL

// Display the linked list


struct Node* temp = head; // Temporary pointer to traverse the list

while (temp != NULL) {

printf("%d -> ", temp->data); // Print the data of each node

temp = temp->next; // Move to the next node

printf("NULL\n");

return 0;

You might also like