You are on page 1of 5

//Header Files

#include<stdio.h>
#include<stdlib.h>
//Linked list is collection of nodes, every node is having two parts as data and next
//We need to create structure for a node as following.
struct node
{
int data;
struct node *next;
};
//start is address of first node
struct node *start=NULL;
//function definition for inserting a new node into single linked list
void insert()
{
//declare node temp, r, s of type struct node, temp is node to be inserted
struct node *temp,*r,*s;
int info;
//allocate dynamic memory to temp, malloc( ) will return address to temp at run time
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter value to be inserted ");
scanf("%d",&temp->data);
//temp->data is data part of temp node
temp->next=NULL;
//temp->next is next part of temp node
if(start==NULL)
//Before performing insertion, first check whether linked list is empty or not. If it is empty, new
//node temp will become start node
start=temp; //make temp node as start node
else //linked list is not empty
{
//In any linked list, we have control over values, not on addresses, so input a value from
//user after which you want to insert temp, that value is info
printf("\n Enter the value after which you want to insert a new value ");
scanf("%d",&info);
r=start; //as we need to find a node whose data part is equal to info,
//so we have taken r node. By making r as start, we will drag r till its data part doesn’t match
//with info
while(r->data!=info) //loop will continue till r's data part is not matched with info
r=r->next; //we will get r here, r=r->next means dragging r till loop condition is true
s=r->next; //we will get s here, immediate node of r will be s by s=r->next
r->next=temp; //r and s are ready, now insert temp, temp will be in between r and s
temp->next=s;
}//end of else
printf("\n New value %d is inserted", temp->data);
}//end of insert

void del( ) //function definition for deleting a node from linked list
{
//declaring nodes of type struct, temp is node which is supposed to be deleted
struct node *temp,*r,*s;
int info;
if(start==NULL) //check whether linked list is empty or not. If empty, we can't perform del()
printf("\n Linked List is empty");
else //there are nodes in linked list
{
//we don’t know address of node to be deleted, so search that node with the help of data part
printf("\n Enter the value you want to delete ");
scanf("%d",&info); //info is the value of node which is to be deleted
//search for temp, drag temp till its data part doesn’t match with info, so first make temp as start
temp=start;
while(temp->data!=info) //loop will continue till temp's data part is not matched with info
temp=temp->next;//we will get temp here, temp=temp->next means dragging temp till loop
//condition is true
//Now after getting temp, check where temp is?
if(temp==NULL) //if temp is NULL, means temp is last node
{
printf("\n %d is deleted", temp->data);
r=start; //we need to delete temp, but before deleting temp, we need to search a node prior to
//temp, as next part of node prior to temp will be NULL, so search it. Suppose r is that node,
//make it start and drag r till r->next is not temp
while(r->next!=temp) //loop will continue till r's next is not temp
r=r->next; //drag r
r->next=NULL; //as we got r, its prior to temp, make r's next as NULL
temp->next=NULL; //as we are going to free temp, make its next part as NULL
}
else if(temp==start) //if temp is start means temp is starting node
{
printf("\n %d is deleted" ,temp->data);
start=start->next;
//after removing starting node, new start will be updated as start->next
temp->next=NULL; //as we are going to free temp, make its next part as NULL
}
else //temp is not first, not last, it is at somewhere middle
{
printf("\n %d is deleted", temp->data);
s=start; //we need to find a node prior to temp, as temp's next node will be that prior node's next.
//Consider that node as s, make s as start. Drag s till next part of s is not temp
while(s->next!=temp) //loop will continue till s's next is not temp
s=s->next; //drag s
s->next=temp->next; //make temp's next as s'next
temp->next=NULL; //as we are going to free temp, make its next part as NULL
} //end of else
free(temp); //free temp node
} //end of outer else
} //end of del( )

void display( ) //function definition for displaying nodes


{
struct node *temp;
if(start==NULL) //check whether nodes are present in linked list or not
printf("\n Linked List is empty");
else //linked list is not empty, display data part
{
temp=start;
//As we need to display all node's data part, we have taken temp node and make it as start node
while(temp!=NULL) //loop will continue till last node
{
printf("\n %d", temp->data); //display temp->data for every node
temp=temp->next; //drag temp
} //end of while
} //end of else
} //end of display( )

void main()
{
int choice;
do //menu driven program
{
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n Enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert( ); //function call
break;
case 2 : del( ); //function call
break;
case 3 : display( ); //function call
break;
case 4 : exit(0);
break;
}//end of switch
}while(choice!=4); //end of do while
} //end of main( )

You might also like