Singly Linked List in C – Data Structures

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.

In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.

Below is the implementation of a Singular Linked List in C.

#include<stdio.h>
#include<stdlib.h>
struct NODE
{
	int data;
	struct NODE *address;
};
typedef struct NODE node;
node *root=NULL;

void add_at_front()
{
	int n;
	node *NN;
	NN=(node *)malloc(sizeof(node));
	printf("Enter the value :");
	scanf("%d",&amp;n);
	NN->data=n;
	NN->address=root;
	root=NN;
}

void add_at_last()
{
	int n;
	node *T,*NN;
	NN=(node *)malloc(sizeof(node));
	printf("Enter the value :");
	scanf("%d",&amp;n);
	NN->data=n;
	for(T=root;T->address!=NULL;T=T->address);
	NN->address=NULL;
	T->address=NN;
}

void add_at_any()
{
	if(root==NULL)
	{
		printf("Link List is empty \n");
	}
	else
	{
		int pos,i,n;
		node *T,*NN;
		NN=(node *)malloc(sizeof(node));
		printf("Enter the position to insert :");
		scanf("%d",&amp;pos);
		printf("Enter the value :");
		scanf("%d",&amp;n);
		NN->data=n;
		T=root;
		for(i=1;i<pos;i++)
		{
			if(T->address==NULL)
			{
				printf("Out of Range !");
			}
			T=T->address;
		}
		NN->address=T->address;
		T->address=NN;
	}
}

void del_front()
{
	if(root==NULL)
	{
		printf("Link List is empty \n");
	}
	else
	{
		node *T;
		T=root;
		root=root->address;
		free(T);
	}
}

void del_last()
{
	if(root==NULL)
	{
		printf("Link List is empty \n");
	}
	else
	{
		node *T,*K;
		for(T=root;T->address->address!=NULL;T=T->address);
		K=T->address;
		T->address=NULL;
		free(K);
	}
}

void del_any()
{
	if(root==NULL)
	{
		printf("Link List is empty \n");
	}
	else
	{
		node *T,*K;
		int i,pos;
		printf("Enter the position to delete :");
		scanf("%d",&amp;pos);
		T=root;
		for(i=1;i<pos-1;i++)
		{
			T=T->address;
		}
		K=T->address;
		T->address=K->address;
		free(K);
	}
}

void display()
{
	if(root==NULL)
	{
		printf("Link List is empty \n");
	}
	else
	{
		node *T;
		for(T=root;T->address!=NULL;T=T->address)
		{
			printf("%d",T->data);
			printf("->");
		}
		printf("NULL \n");
	}
}

void search()
{
	int found=0,n;
	node *T;
	printf("Enter the value of the node to search :");
	scanf("%d",&amp;n);
	for(T=root;T->address!=NULL;T=T->address)
	{
		if(T->data==n)
		{
			found=1;
		}
	}
	if(found==0)
	{
		printf("The node with value %d is not present \n",n);
	}
	else
	{
		printf("The node with value %d is present \n",n);
	}
}

void main()
{
	while(1)
	{
		int ch;
		printf("1=Insert at Front \n2=Insert at last \n3=Insert at any position \n");
		printf("4=Delete at front \n5=Delete at last \n6=Delete at any position \n");
		printf("7=Display \n8=Search \n9=Exit \n");
		printf("Enter choice :");
		scanf("%d",&amp;ch);
		switch(ch)
		{
			case 1:
				add_at_front();
				break;
			case 2:
				add_at_last();
				break;
			case 3:
				add_at_any();
				break;
			case 4:
				del_front();
				break;
			case 5:
				del_last();
				break;
			case 6:
				del_any();
				break;
			case 7:
				display();
				break;
			case 8:
				search();
				break;
			case 9:
				exit(0);
				break;
			default:
				printf("Invalid Choice \n");
				break;
		}
	}
}

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *