forked from lugnitdgp/basic_datastructure_programs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doubly_linked_list.cpp
196 lines (172 loc) · 3.09 KB
/
doubly_linked_list.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<stdio.h>
#include<malloc.h>
struct Node
{
int data;
struct Node *prev;
struct Node *next;
};
void insertpos(int,int);
void insertbegin(int);
void insertend(int);
struct Node *head=NULL;
void createlist(int n)
{
struct Node *temp=head;
temp=(struct Node*)malloc(sizeof (struct Node));
int i=1;
printf("\nEnter The Data For Node %d:",i);
scanf("%d",&temp->data);
if(head==NULL)
{
temp->prev=NULL;
temp->next=NULL;
head=temp;
}
i++;
while(i<=n)
{
struct Node *temp1=temp;
temp=(struct Node*)malloc(sizeof(struct Node*));
printf("\nEnter The Data for Node %d:",i);
scanf("%d",&temp->data);
temp1->next=temp;
temp->prev=temp1;
i++;
}
temp=NULL;
}
void display()
{
struct Node *temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void search(int s,int n)
{
int pos,f=0;
struct Node *temp=head;
while(temp!=NULL)
{
if(temp->data==s)
{
printf("Element Is Present In List:");
f=1;
break;
}
temp=temp->next;
}
if(f==0)
{
printf("Number Not Present");
printf("\nEnter Position For Element:");
scanf("%d",&pos);
if(pos==1)
insertbegin(s);
if(pos>1&&pos<n)
insertpos(pos,s);
if(pos==n+1)
insertend(s);
}
}
void insertpos(int pos,int s)
{
struct Node *temp=head;
int i=1;
struct Node *temp1;
while(i<pos)
{
temp1=temp;
temp=temp->next;
i++;
}
struct Node *new_node;
new_node=(struct Node*)malloc(sizeof(struct Node*));
new_node->data=s;
temp1->next=new_node;
new_node->prev=temp1;
new_node->next=temp;
temp->prev=new_node;
display();
}
void insertbegin(int s)
{
struct Node *new_node;
new_node=(struct Node*)malloc(sizeof(struct Node*));
new_node->data=s;
new_node->prev=NULL;
new_node->next=head;
head->prev=new_node;
head=new_node;
display();
}
void insertend(int s)
{
struct Node *temp=head;
struct Node* prev1;
while(temp!=NULL)
{
prev1=temp;
temp=temp->next;
}
struct Node *new_node;
new_node=(struct Node*)malloc(sizeof(struct Node*));
new_node->data=s;
new_node->prev=prev1;
new_node->next=NULL;
temp->next=new_node;
temp=new_node;
display();
}
void delete_node(int del)
{
struct Node *temp=head;
if (head->data==del)
{
if(head->next==NULL){
free(temp);
head=NULL;
}
else{
head->next->prev=NULL;
head=head->next;
free(temp);
}
}
else{
while(temp->next!=NULL){
if(temp->data==del){
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
else temp=temp->next;
}
if(temp->data==del){
temp->prev->next=NULL;
free(temp);
}
}
display();
}
int main()
{
int n,s;
printf("To Create List");
printf("\nEnter The Number Of Elements In List: ");
scanf("%d",&n);
createlist(n);
//printf("%d",head->data);
display();
printf("\nEnter A number To search");
scanf("%d",&s);
search(s,n);
printf("\nEnter A Number To delete");
int del;
scanf("%d",&del);
delete_node(del);
return 0;
}