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

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.

  1. Singly Linked List
  2. Singly Circular Linked List

Singly Linked list

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.

Singly Linked List
Singly Linked List

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

Singly Linked List Data Structure
Singly Linked List Data Structure

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?

1. Declaring the Linked List
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.
typedef struct LinkedList *node;
node Createnode(){
node temp;
temp = (node)malloc(sizeof(struct Linkedlist));
temp->next=NULL;
return temp;
}

3. Add Node to a linked list
To add the node to the linked list is very important. For This, we require to check different conditions
Condition 1: If the Linked list is empty then add a node to a Linked list and assign head to First Node.
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;
p = head;
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:

Singly Linked list

Singly Circular Linked List

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
Singly Circular 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);
}
 

Output:

Singly Linked list

Advantages of Singly Linked List

1. Insertion and deletion of an element are easy.
2. Space is not wasted as we allocate memory according to requirement.
3. Size is not fixed.
4. We can be extended or reduced according to requirement.
5. Less expensive

Disadvantages of Singly Linked List

1. Different time requires to access each element.
2. Is we want to access particular element then we have to go through all the elements comes before that element.
3. One way traversal.
4. Difficult to sort elements in Linked list.

Write A Comment