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

Sunday, May 22, 2016

WAP to implement doubly 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;
struct node *prev;
}*head,*temp,*temp1;
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;
temp1=head->next;
while(temp1->next!=head)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->prev=temp1;
temp->next=head;
}
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=head->next;
for(i=1;i<p-1 && temp1!=NULL;i++)
{
temp1=temp1->next;
}
if(temp1!=NULL)
{
temp->next=temp1->next;
temp1->next->prev=temp;
temp1->next=temp;
temp->prev=temp1;
}
}
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=head->next;
while(temp1->rno!=x  && temp1!=NULL)
{
temp1=temp1->next;
}
if(temp1!=NULL)
{
temp->next=temp1->next;
temp1->next->prev=temp;
temp1->next=temp;
temp->prev=temp1;
}
}
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(head->next==NULL)
{
head->next=temp;
temp->next=head;
head->prev=temp;
}
else
{
temp->next=head->next;
temp->next->prev=temp;
head->next=temp;
}
temp->prev=head;
}
void delfirst()
{
temp=head->next;
head->next=temp->next;
temp->next->prev=head;

}
void delend()
{
temp=head->next;
while(temp->next->next!=head)
{
temp=temp->next;
}
temp->next=head;
}
void delnode(int x)
{
temp=head->next;
while(temp->next->rno!=x && temp1!=NULL)
{
temp=temp->next;
}
if(temp!=NULL)
{
temp->next=temp->next->next;
temp->next->prev=temp;
}
else
printf("\nNot Found");
}
void delatp(int p)
{
int i;
temp=head->next;
for(i=1;i<p-1 && temp!=NULL;i++)
{
temp=temp->next;
}
if(temp!=NULL)
{
temp->next=temp->next->next;
temp->next->prev=temp;
}
else
printf("\nNot Found");
}
void display()
{
temp=head->next;
do
{
printf("\n|Rollno: %d| ",temp->rno);
printf(" |Name: %s| ",temp->nm);
printf(" |Gender: %c| ",temp->gen);
printf(" |Age: %d| ",temp->age);
temp=temp->next;
}while(temp!=head);
getch();
}
void main()
{
int n,r,a,ch,p,x;
char s[50],g;
clrscr();
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
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);
}