Insertion at first
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *newNode = (struct NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
newNode->next = head;
head = newNode;
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
//Displays the nodes in the list
display();
}
Insertion at last
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
//Displays the nodes in the list
display(); }
Insertion in between
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
struct node *p;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void addNodeposition(int data,int pos)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
int i;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
for(i=1;i<pos-1;i++){
temp=temp->next;}
if( pos==1)
{
newNode->next=head;
head=newNode}
else
{
newNode->next=temp->next;
temp->next=newNode;
}
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
int pos;
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
display();
printf("\nenter the position between 2 to 5\t");
scanf("%d",&pos);
addNodeposition(0,pos);
//Displays the nodes in the list
display(); }
Insertion after some value
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
struct node *p;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void addNodeposition(int data,int value)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
int i;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while(temp != NULL && temp->data != value){
temp=temp->next;}
newNode->next=temp->next;
temp->next=newNode;
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
int value;
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
display();
printf("\nenter the value\t");
scanf("%d",&value);
addNodeposition(0,value);
//Displays the nodes in the list
display(); }
Deletion at first
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
struct node *p;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void deleteNode()
{
struct NODE *temp=head;
if (head == NULL)
printf( “List is Empty”);
else
{
head = head -> next;
free(temp);
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
int value;
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
display();
deleteNode();
//Displays the nodes in the list
display(); }
Deletion at last
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
struct node *p;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void deleteNode()
{
struct NODE *current,*previous;
current = head;
if(head==NULL)
return;
if(head->next==NULL){
head=NULL;
free(current);
return;
}
while(current -> next != NULL)
{
previous=current;
current = current -> next;
}
previous -> next = NULL;
free(current);
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
int value;
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
display();
deleteNode();
//Displays the nodes in the list
display();
deleteNode();
display();
deleteNode();
display();
deleteNode();
display();
display();
deleteNode();
display();
deleteNode();
display();
}
Deletion in between
#include <stdio.h>
#include <stdlib.h>
struct NODE{
int data;
struct NODE *next;
};
//Represent the head the singly linked list
struct NODE *head=NULL;
void addNode(int data)
{
struct NODE *temp = head;
struct NODE *newNode = (struct
NODE*)malloc(sizeof(struct NODE));
newNode->data = data;
struct node *p;
//Checks if the list is empty
if(head == NULL)
{
head = newNode;
newNode->next = NULL; }
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
void deleteNodeafter(int pos)
{
struct NODE *temp=head,*previous;
temp = head ;
int i;
if (head == NULL)
printf( “List is Empty”);
else
{
if(pos==1)
{
head=head->next;
free(temp);
}
else
{
for(i=1;i<pos;i++){
previous=temp;
temp=temp->next;}
previous->next=temp->next;
free(temp);
}
}
}
void display()
{
struct NODE *ptr = head;
if(head == NULL)
{
printf("Linked list is empty\n");
return;
}
printf("Nodes of the linked list...\n");
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next; // Move to the next node
}
}
void main()
{
int pos;
//Add nodes to the list
addNode(10);
addNode(25);
addNode(8);
addNode(4);
addNode(34);
display();
printf("enter position between 1 to 5 \t");
scanf("%d",&pos);
deleteNodeafter(pos);
//Displays the nodes in the list
display(); }
Reverse linked list
struct Node* reverseLinkedList(struct Node* head) {
struct Node *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev; // prev is the new head
}