Singly Linked List:
A singly linked list is a type of data structure that is used to store a collection of
elements called nodes. Each node contains a data item and a reference (address) to the
next node in the sequence. The list is singly linked because each node points only to the
next node in the sequence, not to the preceding one. The first node is referred to as
the "head," and the last node, which has a NULL reference.
Structure of a Node
C code
struct Node {
int data; // The data part
struct Node* next; // Pointer to the next node
};
Basic Operations of singly linked list
Here are the basic operations that can be performed on a singly linked list:
1. Insertion at the beginning
C Implementation:
Basic structure for Single Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node *next;
};
struct node *head=NULL;
void insert_at_beg()
struct node *newnode;
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
if(head==NULL)// Assume if Linked List is Empty.
newnode->next=NULL;
head=newnode;
Else// Assume Linked List contain already Head Node
newnode->next=head;
head=newnode;
printf(" \n Node added at Beginning:");
2. Insertion At the end
C Implementation:
void insert_at_end()
struct node *newnode;
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
newnode->next=NULL;
if(head==NULL)
head=newnode;
struct node *temp=head;
while(temp->next !=NULL)
{
temp=temp->next;
temp->next=newnode;
printf(" \n Node added at end:");
3. Insertion at the position
C Implementation:
void insert_at_position()
{
int pos;
int key;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the Data value:");
scanf("%d",&key);
newnode->data=key;
newnode->next=NULL;
printf("\n Enter the Position Where you wann add node:");
scanf("%d",&pos);
struct node *temp=head;
for(int i=1;i<pos-1;i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
printf(" \n Node added at given Position:");
4) Deletion from the beginning
C Implementation:
void delete_at_start()
struct node *temp=head;
if(head==NULL)
printf("linked List is Empty");
if(head->next==NULL)
// head=NULL;
free(head);
else
head=temp->next;
free(temp);
printf(" first Node is deleted:\n");
5) Deletion from the end
void delete_at_end()
struct node *temp=head;
if(head==NULL)
printf("linked List is Empty");
if(head->next==NULL)
free(head);
else
while(temp->next->next !=NULL)// Traverse upto 2 nd last node
temp=temp->next;
free(temp->next);
temp->next=NULL;
printf(" Last Node is deleted:\n");
6) Deletion at the position
Algorithm
1. Check if the list is empty or if the position is invalid; if so, return.
2. Traverse the list to find the node at the desired position and its previous node.
3. Update the next pointer of the previous node to skip the node at the desired
position.
4. Free the memory of the node at the desired position.
void delete_at_pos()
{
struct node *temp=head;
int loc;
printf("enter the Position where u wann Delete Node:");
scanf("%d",&loc);
for(int i=1;i<loc-1;i++)
temp=temp->next;
temp->next=temp->next->next;
printf("node deleted at Location %d:", loc);
7) Traversal
Algorithm
1. Start from the head node.
2. Visit each node and perform the desired action (e.g., print the node's data).
3. Move to the next node.
4. Continue until reaching a node with a next pointer set to NULL.
void display_sll()
struct node *temp=head;
printf("\n List Elements are:\n");
while(temp !=NULL)
printf("%d-->",temp->data);
temp=temp->next;
}
8) Searching
void search_sll()
int item;
printf("Enter the Searched Data Item Value:\n");
scanf("%d",& item);
struct node *temp=head;
while(temp !=NULL)
if(temp->data==item)
printf("Item present in SLL");
exit(0);
temp=temp->next;
printf("Item not Found in SLL");
void main()
// In main function we are calling only this Function
insert_at_beg();
insert_at_end();
insert_at_end();
insert_at_end();
insert_at_end();
insert_at_position();
display_sll();
delete_at_start();
display_sll();
delete_at_end();
display_sll();
delete_at_pos();
display_sll();
search_sll();
Drawback of Single Linked List
No Random Access: Direct access to individual elements is not possible; one has
to iterate from the head node to access a particular node.
Forward Navigation Only: You can only traverse in one direction, from the head
of the list to the end.