#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#
define NULL0
typedef
struct list
{
int
data;
struct
list *next;
struct
list *prev;
}node;
node
*head = NULL;
node
*prev = NULL;
node*
create_node()
{
node
*new1;
new1
=(node *) malloc(sizeof(node));
printf("\nEnter
data:");
scanf("%d",&new1->data);
new1->next
= NULL;
new1->prev
= NULL;
return(new1);
}
void
insert_beg()
{
node
*new1;
if(head
== NULL)
{
printf("\nlist
is empty:");
new1
= create_node();
head
= new1;
}
else
{
new1
= create_node();
new1->next
= head;
new1->prev
= NULL;
head
= new1;
}
}
void
insert_after()
{
int
insdata;
node
*new1,*temp;
printf("\nEnter
data after which to insert:");
scanf("%d",&insdata);
temp
= head;
while(temp
!= NULL)
{
if(temp->data
== insdata)
{
new1
= create_node();
new1->next
= temp->next;
temp->next
= new1;
return;
}
else
temp
= temp->next;
}
}
void
search()
{
int
insdata,b=1;
node
*temp;
printf("\nEnter
data to be searched:");
scanf("%d",&insdata);
temp
= head;
while(temp
!= NULL)
{
if(temp->data
== insdata)
{
printf("\nData
found in %d node",b);
return;
}
else
{
temp
= temp->next;
b++;
}
}
printf("Data
not found");
}
void
display()
{
node
*temp;
temp
= head;
while(temp
!= NULL)
{
printf("\n%d",temp->data);
temp
= temp->next;
}
}
node
* find_prev(int x)
{
node
*temp,*prev;
temp
= head->next;
prev
= head;
while(temp->next
!= NULL && temp->data != x)
{
prev
= temp;
temp
= temp->next;
}
printf("\nprevious
data = %d",*prev);
return(prev);
}
void
deletion()
{
node
*temp,*temp1;
int
x;
printf("\nEnter
the data to be deleted:");
scanf("%d",&x);
temp
= head;
temp1=head->next;
if(temp->data
== x)
{
head
= temp->next;
free(temp);
}
else
{
while(temp1!=NULL)
{
if(temp1->data
== x)
{
temp->next
= temp1->next;
free(temp1);
return;
}
else
{
temp1=temp1->next;
temp=temp->next;
}}}}
void
main()
{
int
ch,ch1,x;
clrscr();
do
{
printf("\nMENU");
printf("\n1.create
a node\n2.insert a node\n3.search\n4.Display\n5.Find
previous\n6.Delete\n7.exit");
printf("\nEnter
choice");
scanf("%d",&ch);
switch(ch)
{
case
1: head = create_node();
break;
case
2: printf("1.Insert at begining\n2.insert after");
printf("Enter
choice");
scanf("%d",&ch1);
if(ch1
== 1)
insert_beg();
else
insert_after();
break;
case
3: search();
break;
case
4: display();
break;
case
5: printf("\nEnter the data to be found");
scanf("%d",&x);
find_prev(x);
break;
case
6: deletion();
break;
case
7: exit(0);
}
printf("\n\n");
}while(ch
!= 7);
getch();
}
Output:
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice1
Enter
data:2
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice2
1.Insert
at begining
2.insert
afterEnter choice1
Enter
data:4
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice2
1.Insert
at begining
2.insert
afterEnter choice2
Enter
data after which to insert:5
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice4
4
2
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice3
Enter
data to be searched:2
Data
found in 2 node
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice6
Enter
the data to be deleted:4
MENU
1.create
a node
2.insert
a node
3.search
4.Display
5.Find
previous
6.Delete
7.exit
Enter
choice4
2
No comments:
Post a Comment