-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedlist.c
99 lines (91 loc) · 1.81 KB
/
DoublyLinkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
struct node * prev;
};
struct node*head=NULL;
void display(){
struct node* temp;
if(head == NULL){
printf("list is empty");
return;
}
temp=head;
while(temp!=0){
printf("%d",temp->data);
temp=temp->next;
}
}
void insert(){
int x;
printf("enter value:");
scanf("%d",&x);
struct node* hpptr;
struct node* prev;
struct node* pnew;
pnew=(struct node*)malloc(sizeof(struct node));
pnew->data=x;
pnew->next=NULL;
pnew->prev=NULL;
if(head== NULL){
head=pnew;
return;
}
if(head->data>x){
pnew->next=head;
head->prev=pnew;
head=pnew;
return;
}
hpptr=head;
while (hpptr->next != NULL && hpptr->next->data < x)
{
hpptr=hpptr->next;
pnew->next=hpptr->next;
}
if(hpptr->next != NULL){
hpptr->next->prev=pnew;
}
hpptr->next=pnew;
pnew->prev=hpptr;
return;
}
void delete(int x){
struct node* temp;
struct node* hpptr;
if(head->data == x){
temp=head;
head=head->next;
head->prev=NULL;
free(temp);
}
hpptr=head;
while(hpptr->next != NULL && hpptr->next->data!=x){
hpptr=hpptr->next;
}
if(hpptr->next == NULL){
printf("element not found");
}
temp=hpptr->next;
hpptr->next=temp->next;
temp->next->prev=hpptr;
free(temp);
}
int main (){
insert();
insert();
insert();
insert();
display();
printf("\n");
delete(20);
printf("\n");
display();
printf("\n");
insert();
display();
return 0;
}