typedef struct student node;
void Create();
void Display(node *);
void Count();
void Insert();
void Del();
void Search();
void Copy();
void Reverse();
/*Structure Body*/
struct student
{
int rollno;
struct student *prev,*next;
}*first,*first1,*newrec,*newrec1,*last;
void main()
{
int choice=0;
clrscr();
while(choice!=9|| choice>9)
{
printf("\n===============MENU=================");
printf("\n| 1. Create Doubly Linked List |");
printf("\n| 2. Display |");
printf("\n| 3. Count |");
printf("\n| 4. Insert |");
printf("\n| 5. Delete |");
printf("\n| 6. Search |");
printf("\n| 7. Copy |");
printf("\n| 8. Reverse |");
printf("\n|9. Exit |");
printf("\n====================================");
printf("\n Enter the Choice :->");
scanf("%d",&choice);
switch(choice)
{
case 1: Create();
break;
case 2: Display(first);
break;
case 3: Count();
break;
case 4: Insert();
break;
case 5: Del();
break;
case 6: Search();
break;
case 7: Copy();
break;
case 8: Reverse();
break;
case 9: exit(1);
break;
default:
printf("\n invalid choice entery !");
}
}
getch();
}
void Create()
{
node *ptr;
newrec=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec->rollno);
newrec->next=NULL;
newrec->prev=NULL;
if(first==NULL)
first=newrec;
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newrec;
newrec->prev=ptr;
}
last=newrec;
}
void Display(node *first)
{
node *ptr;
int srno=0;
ptr=first;
printf("\n------------------------");
printf("\nSr. No. Record Value ");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->next;
}
printf("\n------------------------");
}
void Count()
{
int c=0;
newrec=first;
while(newrec!=NULL)
{
newrec=newrec->next;
c++;
}
printf("\n Total Number of Records are : %d ",c);
}
void Insert()
{
int i=1,choice=0;
node *ptr;
printf("\n1.Insert Before first \n2.Insert in the last \n3.Insert in between \nEnter Choice ");
scanf("%d",&choice);
newrec=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec->rollno);
ptr=first;
if(choice==1)
{
if(ptr==first || ptr==NULL)
{
newrec->next=first;
first->prev=newrec;
newrec->prev=NULL;
first=newrec;
}
}
else if(choice==2)
{
while(ptr->next!=NULL)
ptr=ptr->next;
newrec->prev=ptr;
ptr->next=newrec;
newrec->next=NULL;
}
else if(choice==3)
{
int p;
printf("\n Enter position at which you want to insert record :");
scanf("%d",&p);
//In insertion we need to goto one position before the place where we
//want to insert new node thats why p-1
while(i<(p-1))
{
ptr=ptr->next;
i++;
}
newrec->next=ptr->next;
newrec->prev=ptr;
ptr->next->prev=newrec;
ptr->next=newrec;
}
else
printf("\n You have entered invalid choice ");
}
void Del()
{
int choice;
node *ptr,*prevptr;
printf("\n1.Delete first \n2.Delete last \n3.Delete node from between :");
scanf("%d",&choice);
ptr=first;
if(first==NULL)
{
printf("\n There is no node to delete ");
return;
}
if(choice==1)
{
first=first->next;
first->prev=NULL;
free(ptr);
}
else if(choice==2)
{
ptr=first;
while(ptr->next!=NULL)
{
prevptr=ptr;
ptr=ptr->next;
}
prevptr->next=NULL;
free(ptr);
}
else if(choice==3)
{
int p,i=1;
printf("\n Enter position of the node that you want to delete :");
scanf("%d",&p);
//In delete we need to go to the node that we want to delete
//ptr=ptr->next by below refer to the node that to be deleted
while(inext;
i++;
}
prevptr->next=ptr->next;
ptr->next->prev=prevptr;
free(ptr);
}
}
void Search()
{
int data;
node *ptr;
printf("\n Enter the Student's Roll no that you want to search : ");
scanf("%d",&data);
ptr=first;
while(ptr!=NULL)
{
if(data==ptr->rollno)
{
printf("\n Record found !");
break;
}
else
ptr=ptr->next;
}
if(ptr==NULL)
printf("\n Requested Record not found !");
}
void Copy()
{
node *ptr,*ptrcopy;
ptr=first;
while(ptr!=NULL)
{
newrec1=(node *)malloc(sizeof(node));
newrec1->next=NULL;
newrec1->prev=NULL;
newrec1->rollno=ptr->rollno;
if(first1==NULL)
first1=newrec1;
else
{
ptrcopy=first1;
while(ptrcopy->next!=NULL)
ptrcopy=ptrcopy->next;
ptrcopy->next=newrec1;
newrec1->prev=ptrcopy;
}
ptr=ptr->next;
}
printf("\n Original List ");
Display(first);
printf("\n Copied List ");
Display(first1);
}
void Reverse()
{
int srno=0;
node *ptr;
ptr=last;
printf("\n----------Reverse List------------");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->prev;
}
printf("\n----------------------------------");
}