Creating a basic linked list in the C program.

Share

As we know, A linked list is a collection of nodes, where each node consists of two parts, info part or data field where the actual element is to be stored in the list and link part or next field that points to some other node in the list. if you are confused in any way about the linked list, make sure you check this article: Linked List in DSA

Let’s create a basic linked list that contains 3 nodes. Basically, nodes are structure in the C program, with data members for actual elements and a self-referencing pointer to point next node. it basically stores the address of the next node. so, a structure for a node can be implemented as below.

struct Node {
	int data; // actual element
	struct Node* next; // pointer for next node
};

Next, we declare separate variables for each node:

struct Node* head; // first node
struct Node* second; //second node
struct Node* third; // third node

After we declare a variable it’s time to allocate all nodes in the memory heap using dynamic memory allocation.

// allocate 3 nodes in the memory heap
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

Now, we are ready to feed values into these variables:

For the first node:

head->data = 5; // assign data in first node
head->next = second; // Link first node and second node ( next node )

Same for the second node:

second->data = 6; // assign data to second node
second->next = third; // Link second node with the third node ( next to second node )

and for the third node:

third->data = 7; // assign data to third node
third->next = NULL; //assign NULL to last node

At this point, we have successfully created a linked list with three nodes. combining all the above snippets we get:

Complere C program

#include <stdio.h>
#include <stdlib.h>

struct Node {
	int data;
	struct Node* next;
};

int main()
{
	
	struct Node* head;
	struct Node* second;
	struct Node* third;

        // allocate 3 nodes in the memory heap
        head = (struct Node*)malloc(sizeof(struct Node));
        second = (struct Node*)malloc(sizeof(struct Node));
        third = (struct Node*)malloc(sizeof(struct Node));

        head->data = 5; // assign data in first node
        head->next = second; // Link first node and second node ( next node )


       second->data = 6; // assign data to second node
       second->next = third; // Link second node with the third node ( next to second node )


       third->data = 7; // assign data to third node
       third->next = NULL; //assign NULL to last node

       // displaying the result

	printf("head Node at: %d \n",head);
	printf("Information/Data: %d \n",head->data);
	printf("Next Pointer : %d \n",head->next);
	
	printf("\n ------------------\n");

	printf("second Node at: %d \n",second);
	printf("Information/Data: %d \n",second->data);
	printf("Next Pointer : %d \n",second->next);
	
	printf("\n ------------------\n");
	
	printf("third Node at: %d \n",third);
	printf("Information/Data: %d \n",third->data);
	printf("Pointer : %d \n",third->next);

	
	return 0;
}

Output:

Check: How to Insert a node in the singly linked list in C programming?

Share
Sudeep Mishra

Sudeep Mishra

Healing

One thought on “Creating a basic linked list in the C program.

Leave a Reply

Your email address will not be published.

%d bloggers like this: