forked from shreyajainn/Data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublydelettionbefore.cpp
102 lines (88 loc) · 1.06 KB
/
doublydelettionbefore.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
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;
}
}
void delete1()
{
cout<<"enter the element you want to delete"<<endl;
int k;
cin>>k;
struct node *t;
t=start;
int c=0,h=1;
while(t->data!=k)
{
t=t->next;
c++;
}
c++;
if(c==1)
{
cout<<"this is the first node"<<endl;
}
else if(c==2)
{
start=t;
t->prev=NULL;
}
else
{
t->prev->prev->next=t;
t->prev=t->prev->prev;
}
}
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();
}