forked from shreyajainn/Data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublydeletion.cpp
112 lines (104 loc) · 1.29 KB
/
doublydeletion.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
using namespace std;
#include<iostream>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* start =NULL;
struct node* createnode()
{
struct node *t;
t =new(struct node);
cin>>t->data;
t->prev=NULL;
t->next=NULL;
return t;
}
struct node* insertnode()
{
struct node* temp;
temp =createnode();
if(start==NULL)
{
start=temp;
temp->next=NULL;
}
else
{
struct node *t;
t=start;
while(t->next!=NULL)
{
t=t->next;
}
t->next=temp;
temp->prev=t;
}
}
struct node* delete1()
{
struct node *temp;
int k;
cout<<"enter the element you want to delete"<<endl;
cin>>k;
int c=0,h=1;
struct node *p;
p=start;
while(p->next!=NULL)
{
h++;
p=p->next;
}
temp =start;
while(temp->data!=k)
{
temp=temp->next;
c++;
}
c++;
if(c<=h)
{
if(c==h)
{
cout<<"we cant dalete"<<endl;
}
else if(c==h-1)
{
struct node *r;
r=temp;
temp->prev->next=temp;
temp->next=temp->next->next;
}
else
{
temp->next=temp->next->next;
temp->next->prev=temp;
}
}
else
{
cout<<"elememt not found"<<endl;
}
}
void traverse()
{
struct node *r;
r=start;
while(r->next!=NULL)
{
cout<<r->data<<endl;
r=r->next;
}
cout<<r->data<<endl;
}
int main()
{
for(int i=0;i<5;i++)
{
insertnode();
}
delete1();
traverse();
}