Single Linkedlist Operations
Single Linkedlist Operations
Single Linkedlist Operations
Singly linked list can be defined as the collection of ordered set of elements. The number of elements
may vary according to need of the program. A node in the singly linked list consist of two parts: data
part and link part. Data part of the node stores actual information that is to be represented by the node
while the link part of the node stores the address of its immediate successor.
One way chain or singly linked list can be traversed only in one direction. In other words, we can say that
each node contains only next pointer, therefore we can not traverse the list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects are stored in a linked
list as shown in the figure.
In the above figure, the arrow represents the links. The data part of every node contains the marks
obtained by the student in the different subject. The last node in the list is identified by the null pointer
which is present in the address part of the last node. We can have as many elements we require, in the
data part of the list.
There are various operations which can be performed on singly linked list. A list of all such operations is
given below.
Node Creation
struct node
int data;
Insertion
The insertion into a singly linked list can be performed at different positions. Based on the position of
the new node being inserted, the insertion is categorized into the following categories.
The Deletion of a node from a singly linked list can be performed at different positions. Based on the
position of the node being deleted, the operation is categorized into the following categories
Linked List in C: Menu Driven Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 9)
printf("\n\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
printf("\nNode inserted");
}
else
temp = head;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
void randominsert()
int i,loc,item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
{
scanf("%d",&item);
ptr->data = item;
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("\ncan't insert\n");
return;
printf("\nNode inserted");
void begin_delete()
printf("\nList is empty\n");
else
ptr = head;
head = ptr->next;
free(ptr);
void last_delete()
if(head == NULL)
printf("\nlist is empty");
head = NULL;
free(head);
}
else
ptr = head;
while(ptr->next != NULL)
ptr1 = ptr;
ptr1->next = NULL;
free(ptr);
void random_delete()
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
printf("\nCan't delete");
return;
free(ptr);
void search()
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
flag=0;
else
flag=1;
i++;
if(flag==1)
void display()
ptr = head;
if(ptr == NULL)
printf("Nothing to print");
else
while (ptr!=NULL)
printf("\n%d",ptr->data);
Output:
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
8.Show
9.Exit
Enter value?
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
printing values . . . . .
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value?
123
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
1234
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter your choice?
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
6
Enter the location of the node after which you want to perform deletion
Deleted node 2
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
printing values . . . . .
1
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
1
item found at location 1
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit