-
Notifications
You must be signed in to change notification settings - Fork 2
/
deletdoubly.cpp
96 lines (83 loc) · 1.46 KB
/
deletdoubly.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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* next;
node* prev;
};
void push(node** head_ref,int data)
{
node* new_node=new node();
new_node->data=data;
new_node->next=*head_ref;
new_node->prev=NULL;
if(*head_ref!=NULL)
{
new_node->prev=new_node;
}
*head_ref=new_node;
}
/*
void push(node** head_ref,int new_data)
{
node *new_node=new node();
new_node->data=new_data;
new_node->next=*head_ref;
new_node->prev=NULL;
//if 1 2 is list and push 3 then 3 1 2 so
if(*head_ref !=NULL)
new_node->prev=new_node;
*head_ref=new_node;
}
*/
void delet(node** head_ref,node* del)
{
//base case
if(*head_ref==NULL || del==NULL)
{
return;
}
//first node
if(*head_ref==del)
{
*head_ref=del->next;
}
if(del->prev!=NULL)
{
del->next->prev=del->prev;
}
if(del->next!=NULL)
{
del->prev->next=del->next;
}
free(del);
return;
}
void printlist(node* head_ref)
{
while(head_ref!=NULL)
{
// cout<<head_ref->prev<<" "<<head_ref->data<<" "<<head_ref->next<<endl;
cout<<" "<<head_ref->data;
head_ref=head_ref->next;
}
cout<<endl;
}
int main()
{
node* head=NULL;
push(&head,1);
push(&head,2);
push(&head,3);
push(&head,4);
push(&head,5);
printlist(head);
//delet(&head,head);
//printlist(head);
delet(&head,head->next);
printlist(head);
return 0;
}