-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdlist.cpp
More file actions
103 lines (88 loc) · 2.2 KB
/
dlist.cpp
File metadata and controls
103 lines (88 loc) · 2.2 KB
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
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " " << x << endl;
struct node{
node* pre;
int data;
node* next;
};
void insert(int data, node* &ptr){
node* temp = new node();
if(ptr != NULL) ptr->pre = temp;
temp->data = data;
temp->pre = NULL;
temp->next = ptr;
ptr = temp;
}
void search(int data,node* root){
while(root != NULL && root->data != data) root = root->next;
if(root == NULL ) cout << "Number not found" << endl;
else cout << "Number Found" << endl;
}
void rev(node**root){
node* temp = *root;
while(temp != NULL){
node *t1, *t2 = temp;
t1 = t2->pre;
t2->pre = t2->next;
t2->next = t1;
*root = t2;
temp = temp->pre;
}
}
void del(int data, node* &ptr){
node *itr = ptr;
while(itr->data != data) itr = itr->next;
// cout << itr << " " << itr->next << endl;
if(itr->pre == NULL){
ptr = ptr->next;
ptr->pre = NULL;
}
else if(itr->next == NULL){
cout << "here" << endl;
itr->pre->next = itr->next;
}
else{
itr->pre->next = itr->next;
itr->next->pre = itr->pre;
}
free(itr);
}
void display(node* root){
while(root != NULL){
cout << root->data << " ";
root = root->next;
}
cout << endl;
}
int main(){
node* root = NULL;
int flag = 1, dtemp;
while(flag){
cout << "\nEnter 1 to insert 2 to delete 3 to display 4 to reverse 5 to search 0 to exit \n" << endl;
cin >> flag;
switch (flag){
case 1:
cout << "Enter data" << endl;
cin >> dtemp;
insert(dtemp, root); break;
case 2:
cout << "Enter value to delete" << endl;
cin >> dtemp;
del(dtemp, root);
break;
case 3:
display(root);
break;
case 4:
rev(&root);
break;
case 5:
cout << "Enter data" << endl;
cin >> dtemp;
search(dtemp, root);
break;
}
}
return 0;
}