Dsaexp 5
Dsaexp 5
h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data ;
struct node*next ;
struct node*prev ;
};
int main()
{
int ch , value ;
while(1)
{
printf("\n\t 1. CREATE \n\t 2. DISPLAY \n\t 3. INSERT \n\t 4. DELETE \n\t 5.
SEARCH \n\t 6.EXIT \n\t") ;
printf("\n\t ENTER YOUR CHOICE : ") ;
scanf("%d",&ch) ;
switch(ch)
{
case 1 :
create() ;
break ;
case 2 :
display() ;
break ;
case 3 :
{
printf("\n\t 1. INSERT BEGINNING \n\t 2. INSERT END \n\t 3. INSERT POSITION
\n\t ") ;
printf("\n\n\t Enter the choice:");
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
insert_beg() ;
break ;
case 2 :
insert_end() ;
break ;
case 3 :
insert_pos() ;
break ;
default :
printf("\n\t Case is not present !!!") ;
}
}
break ;
case 4 :
{
printf("\n\t 1. DELETE BEGINNING \n\t 2. DELETE END \n\t 3. DELETE POSITION
\n\t ") ;
printf("\n\n\t Enter the choice:");
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
delete_beg() ;
break ;
case 2 :
delete_end() ;
break ;
case 3 :
delete_pos() ;
break ;
default :
printf("\n\t Case is not present !!!") ;
}
}
break;
case 5 :
search() ;
break ;
case 6 :
exit(0) ;
break ;
}
}
return 0 ;
}
void create()
{
newnode=(struct node*)malloc(sizeof (struct node)) ;
printf("\n\t ENTER THE DATA : ") ;
scanf("%d", &num ) ;
newnode->data=num ;
newnode->next=NULL ;
newnode->prev=NULL ;
if(head==NULL)
{
head=newnode ;
}
else
{
ptr=head ;
while(ptr->next!=NULL)
{
ptr=ptr->next ;
}
newnode->prev=ptr ;
ptr->next=newnode ;
}
}
void display()
{
if(head==NULL)
printf("\n\t LIST IS EMPTY !!!!!") ;
else
{
ptr=head ;
while(ptr!=NULL)
{
printf("\t%d <--> ", ptr->data) ;
ptr=ptr->next ;
}
}
}
void insert_beg()
{
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("\n\t ENTER THE DATA : ") ;
scanf("%d", &num ) ;
newnode->data=num ;
newnode->next=NULL ;
newnode->prev=NULL ;
if(head==NULL)
{
head=newnode ;
}
else
{
newnode->next=head ;
head->prev=newnode ;
head=newnode ;
}
}
void insert_end()
{
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("\n\t ENTER THE DATA : ") ;
scanf("%d", &num ) ;
newnode->data=num ;
newnode->next=NULL ;
newnode->prev=NULL ;
if(head==NULL)
{
head=newnode ;
}
else
{
ptr=head ;
while(ptr->next!=NULL)
{
ptr=ptr->next ;
}
ptr->next=newnode ;
newnode->prev=ptr ;
}
}
void insert_pos()
{
int pos , i ;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("\n\t ENTER THE POSITION DATA TO BE INSERTED : ") ;
scanf("%d", &pos ) ;
printf("\n\t ENTER THE DATA : ") ;
scanf("%d", &num ) ;
newnode->data=num ;
newnode->next=NULL ;
newnode->prev=NULL ;
ptr=head ;
for(i=1;i<pos;i++)
{
if(ptr->next==NULL)
{
printf("\n\t NODES ARE LESS THAN POSITION !!!!!!") ;
return 0 ;
}
temp=ptr ;
ptr=ptr->next ;
newnode->next=ptr ;
ptr->prev=newnode ;
temp->next=newnode ;
newnode->prev=temp ;
printf("\n\t NODE IS CREATED ") ;
}
void delete_beg()
{
if(head==NULL)
printf("\n\t NO NODE TO BE DELETED !!!!") ;
else
{
ptr=head ;
head=head->next;
printf("\n\t NODE DELETED = %d", ptr->data) ;
}
free(ptr) ;
}
void delete_end()
{
ptr=head ;
if(head==NULL)
printf("\n\t NO NODE TO BE DELETED !!!!") ;
else
{
while(ptr->next!=NULL)
{
temp=ptr ;
ptr=ptr->next ;
}
printf("\n\t NODE DELETED = %d", ptr->data ) ;
temp->next=NULL ;
free(ptr) ;
}
}
void delete_pos()
{
int pos , i ;
printf("\n\n\t ENTER THE POSITION, DATA TO BE DELETED : ") ;
scanf("%d", &pos ) ;
ptr=head ;
for(i=1;i<pos-1;i++)
{
/*if(ptr->next==NULL)
{
printf("\n\t NODES ARE LESS THAN POSITION !!!!!!") ;
return 0 ;
}
else*/
ptr=ptr->next ;
}
temp=ptr->next ;
ptr->next=temp->next ;
printf("\n\t DATA DELETED = %d",temp->data) ;
free(temp) ;
}
void search()
{
int node , i=1 ;
printf("\n\t ENTER THE NODE TO BE SEARCHED ") ;
scanf("%d", &node) ;
ptr=head ;
while(ptr->next!=NULL)
{
if(ptr->data==node)
printf("\n\tPOSITION OF NODE IS %d",i) ;
ptr=ptr->next ;
i++ ;
}
if(ptr->data!=node)
printf("\n\n\t NODE IS NOT PRESENT.....");
}