-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelink.cpp
72 lines (60 loc) · 1.19 KB
/
doublelink.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
#include <iostream>
using namespace std;
class node{
public:
int data;
node* prev;
node* next;
node(int val){
prev = NULL;
next = NULL;
data =val;
}
};
void insertattail(node* &head,int val){
node* n = new node(val);
if(head == NULL){
head = n;
return;
}
node* trav = head;
while(trav->next!=NULL){
trav = trav->next;
}
trav->next = n;
n->prev = trav;
}
void display(node* &head){
node* trav = head;
while(trav->next!=NULL){
cout<<trav->data<<" ";
trav = trav->next;
}
cout<<trav->data<<" "<<endl;
}
void deletenode(node* &head,int pos){
node* temp =head;
int posk=1;
while(posk<pos){
temp = temp->next;
posk++;
}
if(temp->prev!=NULL)
temp->prev->next = temp->next;
else
head = head->next;
head->prev=NULL;
if(temp->next!=NULL)
temp->next->prev = temp->prev;
delete temp;
}
int main(){
node* head = NULL;
insertattail(head, 2);
insertattail(head, 3);
insertattail(head, 4);
insertattail(head, 5);
display(head);
deletenode(head, 1);
display(head);
}