C program to perform implementation of Linked List

Linked List is an Example of dynamic data structure, they are not required to be stored in adjacent memory locations, Each structure of the linked list consists of the data field and address field which holds the address of its successor.

C program for linked list

Before going through this article we recommend you to understand the concept of linked list, please refer to the following articles

Understanding the Concepts of Linked List-The Easy Way

https://en.wikipedia.org/wiki/Linked_list

In this article, we will learn how to write a C program to create a linked list and perform the following operations on it.

  • Inserting a new node
  • Displaying the Linked List
  • Deleting a node from linked list(We will see this in another article)
  • Check whether the Linked List is empty or not

We are going to make functions for each of the above operation, before that we will see how we can represent a node of a linked list in C programming using Structures.

see below code snippet.

struct node{ int value; struct node *address; } 

see below picture to understand it more clearly

Understanding Linked List concepts

In the above, structure we can consider the whole structure as a node where the integer variable value and pointer address as the value part and address part of the node.

Now we will make each of the above function one by one let’s start by making an check_empty() function to check whether a Linked List is empty, see the below code snippet.

int check_empty(){  if(head==NULL){ return 1; }else{ return 0;} }

The condition for a linked list to be empty is the head node or the starting node address should be NULL which means the address of the starting node is empty, so we will take a pointer variable which will point to the node structure and will store the address of the first node. If it will be NULL then the function will return true or 1 else it will return false or 0.

Now let’s move o to the insert() function which will insert a new node to the linked list before coding let us revise that a node in a linked list contains the address of its next node in its address part except the last node of the linked list which address part is NULL.See the below code snippet.

int insert(int x){ struct node *temp; if(check_empty()){ temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->add = NULL; head = temp; } else{ y = temp; temp = (struct node *)malloc(sizeof(struct node)); y->add = temp; // connecting new node with previous node temp->val = x; temp->add = NULL; } }
 

For insertion, there will be two conditions one when the linked list will be empty and the other when it will be not empty, when it will be empty we will allocate a new memory for our node using the malloc() function and store the memory address in the pointer variable temp now using temp we will access the node structure data members to the newly allocated memory and assign value part as x and the address part as NULL(There will be no next node after the newly inserted node) after that we will store the address of the newly inserted node into the head pointer by assigning it to temp.

When then list will be not empty then we will assign the address of the current node in a pointer variable y and then we will allocate a new memory for the new node using malloc() and will assign the address of the previous node as temp(address of the previous node will contain the address of the next node), assign the current node’s value part as x and address part as NULL.

Now let’s see how to display each element of the linked list we will make a function called display() for it, see the below code snippet.

void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->value); j = j->address; } }
 

In the above example, we will run the while loop until the address becomes NULL the condition for next iteration will be j = j->address which will assign j the address of the next node because in address j the address part contains the address of the next node so j->address shows the address of the next node.

Now let us combine everything and see the whole code.

C program for implementing Linked List

 
#include<stdio.h> #include<stdlib.h> struct node{ int value; struct node *address;  }; int temp; struct node *y,*head; int empty(){ if(head==NULL){ return 1; } else{ return 0; }  } int insert(int x){ struct node *temp; if(empty()){ temp = (struct node *)malloc(sizeof(struct node)); temp->val = x; temp->add = NULL; head = temp; } else{ y = temp; temp = (struct node *)malloc(sizeof(struct node)); y->add = temp; temp->val = x; temp->add = NULL; } } void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->val); j = j->add; } } int main(){ insert(10); insert(20);       insert(15); display(); }

Output of the above program:

Linked List output

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook . You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter at top of this page.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий