The singly linked list data structure is a set of nodes where each node contains two fields ‘data’ and ‘reference’ field.

Page Contents

## Singly linked list Data structure

A singly-linked list data structure is one type of linked list where the singly linked list means we are having a node with data and its next node address.
The singly linked list data structure is the most basic and common data structure. The singly linked list is a set of nodes which are connected to another node sequentially by the reference to the next node.

### Types of Singly linked list

In the data structure, a singly linked list data structure is implemented in two way which is as follows.

This is one implementation for the singly Linked list. It is also simply known as a Singly Linked list.
The Linked list contains a set of the node with two field data and reference. The reference field is address or pointer to the next node.
The name non-circular states that the last node of a linked list does not point to any node which contains it value NULL.

#### Representation for Singly Linked list

The first node in the linked list is referred to as a Head Node. If the Linked list is empty then Head Node points to Null.

The node consists of two parts
1. Data: the value of the node
2. Address: Reference to the next node

Last Node Points to NULL indicates its end (Non-circular).
In the linked list structure, data stores int type data in a node and the next pointer point’s next node of the same type structure.

#### How do you create a singly Linked list in Data structure?

The first step we need to define the node.
To implement a node we use a type of structure to form a node.
Struct node
{
int data;
Struct node * next;
}

2. Creating Node
After that, we create the node in the linked list.
node Createnode(){
node temp;
temp->next=NULL;
return temp;
}

To add the node to the linked list is very important. For This, we require to check different conditions
Condition 2: If the Linked list is not empty (To add Node at the end), then traverse the linked list till the end and add a node at the end.

4. Traverse the node
The linked list can be traversed in a while loop by using the head node as a starting reference:
node p;
while(p != NULL){
p = p->next;
}

#### Singly-linked list program in c

```# define scanf sf
#include<stdio.h>
#include<conio.h>
struct node{
int data;
struct node *next;
};
struct node *start;
void insert_beg(void);
void insert_end(void);
void insert_before(void);
void display(void);
void deletion(void);
void destroy(void);
void main(void)
{
int ch;
start=NULL;
do{
printf("\n 1. insert_beg\n 2. insert_end\n 3. insert_before\n 4. Display\n 5. Deletion\n 6.Destroy\n 7.Exit \n Enter you choice = ");
scanf("%d",&ch);
switch(ch){
case 1:
insert_beg();
break;
case 2:
insert_end();
break;

case 3:
insert_before();
break;

case 4:
display();
break;

case 5:
deletion();
break;

case 6:
destroy();
break;

case 7:
break;
}
}while(ch!=7);
}
void insert_beg(void)
{
struct node *nn;
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
if(start==NULL){
nn->next=NULL;
start=nn;
}
else{
nn->next=start;
start=nn;
}
}
void insert_end(void)
{
struct node *nn ,*temp;
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
if(start==NULL){
nn->next=NULL;
start=nn;
}
else{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
nn->next=NULL;
}
}
void insert_before(void)
{
struct node *nn ,*temp,*ptemp;
int x;

if(start==NULL){
printf("\n SLL is empty--------------");
return;
}
printf("\n Enter data which you want to insert the node = ");
scanf("%d",&x);
if(x==start->data){
insert_beg();
return;
}
temp=start;
while(temp!=NULL && temp->data!=x)
{
ptemp=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("\n %d does not exist:",x);
}
else{
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
ptemp->next=nn;
nn->next=temp;
}
}

void display(void)
{
struct node *temp;
if(start==NULL)
{
printf("\n SLL is empty -----------");
return;
}
temp=start;
while(temp!=NULL)
{
printf("| %d |-> ",temp->data);
temp=temp->next;
}
}

void deletion(void)
{
struct node *ptemp,*temp;
int x;
if(start==NULL)
{
printf("\n SLL is empty");
return;
}
printf("Enter data to delete");
sf("%d",&x);
if(x==start->data)
{
temp=start;
start=start->next;
free(temp);
return;
}
}

void destroy(void)
{
struct node *temp,*dp;
if(start==NULL)
{
printf("\n SLL is empty");
return;
}
temp=start;
while(temp!=NULL)
{
dp=temp;
temp=temp->next;
free(dp);
}
start=NULL;
}

```
##### Output:

This is another way to implement a singly Linked list.
Circular Linked list contains set of the node with two field data and reference like Singly Non-circular linked list only difference is instead of pointing NULL the last node point to the first node which forms a circular linked list.

#### Representation of Singly Circular Linked List

We can see that the last node points to the first node to form a circular list.
Except connecting last node with first node the implementation of Singly Circular linked list is similar to the singly linked list.

#### Singly circular linked list program in c

```# define scanf sf
#include<stdio.h>
#include<conio.h>
struct node{
int data;
struct node *next;
};
struct node *start;
void insert_beg(void);
void insert_end(void);
void insert_before(void);
void display(void);
void deletion(void);
void destroy(void);
void main(void)
{
int ch;
start=NULL;
do{
printf("\n 1. insert_beg\n 2. insert_end\n 3. insert_before\n 4. Display\n 5. Deletion\n 6.Destroy\n 7.Exit \n Enter you choice = ");
scanf("%d",&ch);
switch(ch){
case 1:
insert_beg();
break;
case 2:
insert_end();
break;

case 3:
insert_before();
break;

case 4:
display();
break;

case 5:
deletion();
break;

case 6:
destroy();
break;

case 7:
break;
}
}while(ch!=7);
}
void insert_beg(void)
{
struct node *nn,*temp;
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
if(start==NULL)
{
nn->next=nn;
start=nn;
}
else
{
temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=nn;
nn->next=start;
start=nn;
}
}
void insert_end(void)
{
struct node *nn ,*temp;
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
if(start==NULL)
{
nn->next=nn;
start=nn;
}
else
{
temp=start;
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=nn;
nn->next=start;
}
}
void display(void)
{
struct node *temp;
if(start==NULL)
{
printf("\n SCLL is empty ----");
return;
}
temp=start;
do
{
printf("| %d | -> ",temp->data);
temp=temp->next;
}
while(temp!=start);
}

void destroy(void)
{
struct node *temp,*dp;
if(start==NULL)
{
printf("\n SCLL is empty : -----");
return;
}
temp=start;
do
{
dp=temp;
temp=temp->next;
free(dp);
}while(temp!=start);
start=NULL;

}
void insert_before(void)
{
struct node *nn ,*temp,*ptemp;
int x;

if(start==NULL)
{
printf("\n SCLL is empty: ---- ");
return;
}
printf("\nEnter data which you want to insert the node = ");
scanf("%d",&x);
if(x==start->data)
{
insert_beg();
return;
}
temp=start;
while(temp->data!=x)
{
ptemp=temp;
temp=temp->next;
if(temp==start)
{
printf("\n %d does not exist:",x);
return;
}
}
nn=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data = ");
scanf("%d",&nn->data);
ptemp->next=nn;
nn->next=temp;
}
void deletion(void)
{
struct node *ptemp,*temp,*lp;
int x;
if(start==NULL)
{
printf("\n SCLL is empty----------");
return;
}
printf("\nEnter data to delete = ");
sf("%d",&x);
if(x==start->data)
{
if(start==start->next)
{
temp=start;
start=NULL;
free(temp);
}
else
{
lp=start;
while(lp->next!=start)
{
lp=lp->next;
}
temp=start;
start=start->next;
free(temp);
lp->next=start;
}
return;
}
temp=start;
while(temp->data!=x)
{
ptemp=temp;
temp=temp->next;
if(temp==start)
{
printf("\n %d is does not exist",x);
return;
}
}
ptemp->next=temp->next;
free(temp);
}

```