//Singly linked list
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int info;
struct node *next;
}NODE;
NODE* createlist(NODE *list);
void Display (NODE *list);
void search (NODE *list);
NODE* insertbeg(NODE *list);
NODE* insertbetween(NODE *list);
NODE* insertlast(NODE *list);
NODE* Delpos(NODE *list);
NODE* Delvalue(NODE *list);
//MAIN FUNCTION
void main ()
{
printf(">>Singly Linked List<<");
NODE*list=NULL, *temp;
int ch,n,pos;
list=createlist (list);
Display (list);
search (list);
list=insertbeg (list);
Display (list);
list=insertbetween (list);
Display (list);
list=insertlast (list);
Display (list);
list=Delpos (list);
Display (list);
list=Delvalue (list);
Display (list);
}
//CREATE FUNCTION
NODE* createlist (NODE *list)
{
int n,count;
NODE *temp, *newnode;
printf ("\nHow many nodes you want to enter ? \n");
scanf("%d" ,&n);
for(count=1 ; count<=n; count++)
{
newnode=(NODE*)malloc(sizeof(NODE));
newnode->next=NULL;
printf( "Enter the node data:- " );
scanf ("%d",&newnode->info);
if (list==NULL)
{
list=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return list;
}
void Display (NODE *list)
{
NODE *temp=list;
while(temp!=NULL)
{
printf ("%d",temp->info);
printf("-->");
temp=temp->next ;
}
printf("NULL \n");
}
//Insert AT BEGINING
NODE * insertbeg(NODE *list)
{
int n;
printf("Enter the node you want to insert at first position:");
scanf("%d", &n);
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->info=n;
newnode->next=list;
list=newnode;
return list;
}
//INSERT IN BETWEEN
NODE * insertbetween(NODE *list)
{
NODE *newnode, *temp=list;
int n,i,pos;
printf("Enter the node data and position you want to insert between the node: \t");
scanf("\n%d", &n);
scanf("\n%d", &pos);
newnode=(NODE*)malloc(sizeof(NODE));
newnode->next=NULL;
newnode->info=n;
for(i=1; i<pos-1&&temp->next!=NULL; i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
return list;
}
//INSERT AT LAST
NODE * insertlast(NODE *list)
{
int n;
printf("Enter the node data you want to insert at last position: \t");
scanf("%d", &n);
NODE *newnode, *temp;
newnode=(NODE*)malloc(sizeof(NODE));
newnode->info=n;
newnode->next=NULL;
for(temp=list; temp->next!=NULL; temp=temp->next);
temp->next=newnode;
return list;
}
//SEARCH
void search (NODE *list)
{
int num, flag=0;
NODE *temp;
printf("Enter the element to be search:");
scanf("%d", &num);
for(temp=list; temp!=NULL; temp=temp->next)
{
if(temp->info==num)
{
printf(">>%d is Found<<\n",num);
flag=1;
exit;
}
}
if(flag==0)
printf(">>%d is not found<<\n", num);
}
//DELETE BY POSITION
NODE * Delpos(NODE *list)
{
NODE *temp=list, *temp1;
int i, pos;
printf("Enter node position to delete the node: \t");
scanf("%d", &pos);
if(pos==1)
{
list=temp->next;
free (temp);
return list;
}
for(i=1, temp=list; i<=pos-1 && temp!=NULL; i++)
temp=temp->next;
if(temp==NULL)
{
printf(">>position is out of range<<");
return list;
}
temp1=temp->next;
temp->next=temp1->next;
free (temp1);
return list;
}
//DELETE BY VALUE
NODE* Delvalue(NODE *list)
{
NODE *temp=list, *temp1;
int num;
printf("Enter node data to delete that node \t");
scanf("%d", &num);
if(list->info==num)
{
list=list->next;
free (temp);
return list;
exit;
}
for(temp=list; temp->next!=NULL; temp=temp->next)
if(temp->next->info==num)
{
temp1=temp->next;
temp->next=temp1->next;
free (temp1);
return list;
exit;
}
printf(">>Element is not found<<\n");
return list;
}