/*Doubly Linked List */
#include<malloc.h>
#include<stdlib.h>
struct list_element
{
int d;
struct list_element *next,*prev;
};
typedef list_element node;
void create(node *);
void displayn(node *);
void displayr(node *);
node *search(node *);
node *inserteb(node *);
void insertel(node *);
void inserte(node *);
node *deletef(node *);
void deletee(node *);
void deleteb(node *);
int main()
{
int choice;
node *root,*record,*tag ;
record=(node *)malloc(sizeof(node));
root=record;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display list in forward drection:");
printf("\n Press 3 to display list in reverse direction:");
printf("\n Press 4 to search in the list:");
printf("\n Press 5 to inserte at the begining of the list:");
printf("\n Press 6 to inserte at the end of the list:");
printf("\n Press 7 to inserte between first and last element:");
printf("\n Press 8 to delete the first element:");
printf("\n Press 9 to delete the last elemnt:");
printf("\n Press 10 to delete between the last and first elements:");
printf("\n Press 11 to exit:");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
displayn(root);
break;
case 3:
displayr(root);
break;
case 4:
tag=search(root);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 5:
root=inserteb(root);
break;
case 6:
insertel(root);
break;
case 7:
inserte(root);
break;
case 8:
root=deletef(root);
break;
case 9:
deletee(root);
break;
case 10:
deleteb(root);
break;
case 11:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int i,n;
node *p;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
record->prev=NULL;
for(i=1;i<=n-1;i++)
{
p=record;
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
record->prev=p;
}
record->next=NULL;
return;
}
void displayn(node *record)
{
printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->next;
}
}
void displayr(node *record)
{
printf("\n");
while(record->next!=NULL)
{
// printf("\t %d",record->d);
record=record->next;
}
printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->prev;
}
}
node *search(node *record)
{
int n,flag=0;
node *p;
printf("\n Enter the element to be searched:\n");
scanf("%d",&n);
while(record!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
}
if(flag==1)
return (p);
else
return (NULL);
}
node *inserteb(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=record;
record->prev=p;
p->prev=NULL;
return (p);
}
void insertel(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
while(record->next!=NULL)
{
record=record->next;
}
record->next=p;
p->next=NULL;
p->prev=record;
}
void inserte( node *record)
{
node *p,*tag,*pv;
displayn(record);
printf("\n Enter the element after which insertion has to be done:");
tag=search(record);
if(tag==NULL)
{
printf("\n Cannot be inserted");
}
else
{
pv=tag;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=tag->next;
tag->next=p;
p->prev=pv;
p->next->prev=p;
}
}
node *deletef(node *record)
{
node *p;
p=record->next;
p->prev=NULL;
free(record);
return(p);
}
void deletee(node *record)
{
node *p;
while(record->next!=NULL)
{
record=record->next;
}
p=record->prev;
p->next=NULL;
p->prev=record->prev->prev;
free(record);
}
void deleteb(node *record)
{
node *p,*tag;
displayn(record);
printf("\n Item to be deleted, should be searched:\n");
tag=search(record);
if(tag==NULL)
printf("\n Element not found, try again");
else
{
p=tag->prev;
p->next=tag->next;
p->next->prev=p;
free(tag);
}
}
No comments:
Post a Comment