Showing posts with label singly link list. Show all posts
Showing posts with label singly link list. Show all posts

Wednesday, May 18, 2016

WAP to implement singly link list of various options of insert(beginning, end, at position, after node) and delete(beginning, end, at position, specific node)

#include<stdio.h>
#include<conio.h>
struct node
{
int rno;
char nm[50];
char gen;
int age;
struct node *next;
}*first,*temp,*temp1;
void insert(int r,char s[], char g,int a)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->rno=r;
strcpy(temp->nm,s);
temp->gen=g;
temp->age=a;
temp->next=NULL;
if(first==NULL)
{
first=temp;
}
else
{
temp1->next=temp;
}
temp1=temp;
}
void insend(int r,char s[], char g,int a)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->rno=r;
strcpy(temp->nm,s);
temp->gen=g;
temp->age=a;
temp->next=NULL;
temp1=first;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
}
void insatp(int r,char s[], char g,int a,int p)
{
int i;
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->rno=r;
strcpy(temp->nm,s);
temp->gen=g;
temp->age=a;
temp1=first;
for(i=1;i<p-1 && temp1!=NULL;i++)
{
temp1=temp1->next;
}
if(temp1!=NULL)
{
temp->next=temp1->next;
temp1->next=temp;
}
}
void insaft(int r,char s[], char g,int a,int x)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->rno=r;
strcpy(temp->nm,s);
temp->gen=g;
temp->age=a;
temp1=first;
while(temp1->rno!=x  && temp1!=NULL)
{
temp1=temp1->next;
}
if(temp1!=NULL)
{
temp->next=temp1->next;
temp1->next=temp;
}
}
void insbeg(int r,char s[], char g,int a)
{
temp=(struct node*)malloc(sizeof(struct node)) ;
temp->rno=r;
strcpy(temp->nm,s);
temp->gen=g;
temp->age=a;
if(first==NULL)
{
first=temp;
}
else
{
temp->next=first;
first=temp;
}
}
void delfirst()
{
temp=first;
first=temp->next;

}
void delend()
{
temp=first;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
void delnode(int x)
{
temp=first;
while(temp->next->rno!=x && temp1!=NULL)
{
temp=temp->next;
}
if(temp!=NULL)
temp->next=temp->next->next;
else
printf("\nNot Found");
}
void delatp(int p)
{
int i;
temp=first;
for(i=1;i<p-1 && temp!=NULL;i++)
{
temp=temp->next;
}
if(temp!=NULL)
temp->next=temp->next->next;
else
printf("\nNot Found");
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("\n|Rollno: %d| ",temp->rno);
printf(" |Name: %s| ",temp->nm);
printf(" |Gender: %c| ",temp->gen);
printf(" |Age: %d| ",temp->age);
temp=temp->next;
}
getch();
}
void main()
{
int n,r,a,ch,p,x;
char s[50],g;
clrscr();
do
{
printf("\n1.Insert ");
printf("\n2.delete ");
printf("\n3.display ");
printf("\n4.exit");
printf("\nEnter your choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter rollno: ");
scanf("%d",&r);
fflush(stdin);
printf("Enter name: ");
gets(s);
printf("Enter gender(m/f): ");
scanf("%c",&g);
printf("Enter age: ");
scanf("%d",&a);
printf("\nSelect Option");
printf("\n1.Insert at Begining");
printf("\n2.Insert at Postion");
printf("\n3.Insert after Rollno");
printf("\n4.Insert at End");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insbeg(r,s,g,a);
break;
case 2:
printf("\nEnter Position:");
scanf("%d",&p);
insatp(r,s,g,a,p);
break;
case 3:
printf("Enter Rollno:");
scanf("%d",&x);
insaft(r,s,g,a,x);
break;
case 4:
insend(r,s,g,a);
break;
}

break;
case 2:
printf("\nSelect Option");
printf("\n1.Delete at Begining");
printf("\n2.Delete at Postion");
printf("\n3.Delete Rollno");
printf("\n4.Delete at End");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
delfirst();
break;
case 2:
printf("\nEnter Position:");
scanf("%d",&p);
delatp(p);
break;
case 3:
printf("Enter Rollno:");
scanf("%d",&x);
delnode(x);
break;
case 4:
delend();
break;
}
printf("\n Value Deleted ");
break;
case 3:
display();
break;
case 4:
printf("\nThank you");
break;
default:
printf("\nInvalid choice");
}

}while(n!=4);
}