#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
char name[20];
int roll;
struct student *link;
}student;
student *insbeg(student *head,char *name,int roll)
{
student *temp;
temp=(student *)malloc(sizeof(struct student));
temp->roll=roll;
temp->link=NULL;
strcpy(temp->name,name);
if(head==NULL)
{
head=temp;
return head;
}
temp->link=head;
head=temp;
return head;
}
student *insatend(student *head,char *name,int roll)
{
student *temp,*p;
temp=(student *)malloc(sizeof(struct student));
temp->roll=roll;
temp->link=NULL;
strcpy(temp->name,name);
if(head==NULL)
{
head=temp;
return head;
}
p=head;
while(p->link!=NULL)
p=p->link;
p->link=temp;
return head;
}
void search(student *head,char *name,int roll)
{
student *p=head;
int pos=1;
while(p)
{
if(p->roll==roll && !strcmp(p->name,name))
{
printf("\nItemp found with Name %s \tRoll %d ",p->name,p->roll);
return ;
}
p=p->link;
pos++;
}
printf("\nItemp not found");
}
student *insempty(student *head,char *name,int roll)
{
student *temp;
temp=(student *)malloc(sizeof(struct student));
temp->roll=roll;
temp->link=NULL;
strcpy(temp->name,name);
if(head==NULL)
{
head=temp;
return head;
}
printf("\nlist already contains some data");
return head;
}
void display(student *head)
{
student *p=head;
while(p!=NULL)
{
printf("\nName : %s \tRoll : %d ",p->name,p->roll);
p=p->link;
}
}
student *insertafter(student *head,char *name,int roll,char *nname,int nroll)
{
student *temp,*p=head;
while(p)
{
if(p->roll==roll && !strcmp(p->name,name))
{
printf("\nItemp found with Name %s \tRoll %d ",p->name,p->roll);
temp=(student *)malloc(sizeof(struct student));
temp->roll=nroll;
temp->link=NULL;
strcpy(temp->name,nname);
temp->link=p->link;
p->link=temp;
return head;
}
p=p->link;
}
printf("\nItemp not found");
return head;
}
student *insertbefore(student *head,char *name,int roll,char *nname,int nroll)
{
student *temp,*p=head,*q=head;
temp=(student *)malloc(sizeof(struct student));
temp->roll=nroll;
temp->link=NULL;
temp->link=NULL;
strcpy(temp->name,nname);
if(p->roll==roll && !strcmp(p->name,name))
{
printf("\nItemp found with Name %s \tRoll %d ",p->name,p->roll);
temp->link=p;
head=temp;
return head;
}
while(p)
{
if(p->roll==roll && !strcmp(p->name,name))
{
temp->link=p;
q->link=temp;
return head;
}
q=p;
p=p->link;
}
printf("\nItemp not found");
free(temp);
return head;
}
struct student *deletfirst(student *head)
{
student *temp=head;head=head->link;
free(temp);
return head;
}
student *deletbefore(student *head,char *name,int roll)
{
student *p=head,*q=head;
if(p->roll==roll && !strcmp(p->name,name))
{
printf("\nItem is the first node so can't delete ");
return head;
}
p=head->link;
if(p->roll==roll && !strcmp(p->name,name))
{
free(head);
head=p;
return head;
}
while(p)
{
if(p->link->roll==roll && !strcmp(p->link->name,name))
{
q->link=p->link;
free(p);
return head;
}
q=p;
p=p->link;
}
printf("\nItemp not found");
return head;
}
student *deletafter(student *head,char *name,int roll)
{
student *p=head,*q;
while(p->link)
{
if(p->roll==roll && !strcmp(p->name,name))
{
q=p->link;
p->link=p->link->link;
free(q);
return head;
}
p=p->link;
}
if(p->roll==roll && !strcmp(p->name,name))
{
printf("\nthis is the last item so deletion can't be performed of a node after this");
return head;
}
printf("\nItemp not found");
return head;
}
student *deletkey(student *head,char *name,int roll)
{
student *p=head,*q;
if(p->roll==roll && !strcmp(p->name,name))
{
head=p->link;
free(p);
return head;
}
while(p)
{
if(p->roll==roll && !strcmp(p->name,name))
{
q->link=p->link;
free(p);
return head;
}
q=p;
p=p->link;
}
printf("\nItemp not found");
return head;
}
void main()
{
int roll,ch,nroll;
char name[20],nname[20];
student *head=NULL,*p=head;
printf("\n1.insertion at begining\t2.insertion at end\n3.insertion in empty\t4.insertion in between\n5.display\t\t6.search item\t7.insert after item\t8.insert before item\t9.delet first node\t10.delet before\t11.delet after\t12.delet key\t13.exit");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=insbeg(head,name,roll);
break;
case 2: printf("Enter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=insatend(head,name,roll);
break;
case 3: printf("Enter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=insempty(head,name,roll);
break;
case 5: printf("\nContent of list ");
display(head);
break;
case 6: printf("Enter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
search(head,name,roll);
break;
case 7: printf("After whom \nEnter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
printf("To who\tEnter Name :");
fflush(stdin);
gets(nname);
printf("Enter Roll :");
scanf("%d",&nroll);
head=insertafter(head,name,roll,nname,nroll);
break;
case 8: printf("Before whom \nEnter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
printf("To who\tEnter Name :");
fflush(stdin);
gets(nname);
printf("Enter Roll :");
scanf("%d",&nroll);
head=insertbefore(head,name,roll,nname,nroll);
break;
case 9 :head=deletfirst(head);
break;
case 10:printf("\nbefore whom");
printf("\nEnter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=deletbefore(head,name,roll);
break;
case 11:printf("\nAfter whom");
printf("\nEnter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=deletafter(head,name,roll);
break;
case 12:printf("\nTo whom");
printf("\nEnter Name :");
fflush(stdin);
gets(name);
printf("Enter Roll :");
scanf("%d",&roll);
head=deletkey(head,name,roll);
break;
case 13: exit(1);
}
}
while(head)
{
p=head->link;
free(head);
head=p;
}
}