-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublyLL.cpp
More file actions
138 lines (110 loc) · 2.39 KB
/
Copy pathdoublyLL.cpp
File metadata and controls
138 lines (110 loc) · 2.39 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node* prev;
Node(int d){
this->data=d;
this->next=NULL;
this->prev=NULL;
}
};
void print(Node* &head){
Node* temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int getLen(Node* head){
int len=0;
Node* temp=head;
while(temp!=NULL){
len++;
temp=temp->next;
}
}
int insertAtHead(Node* &head, int d){
Node* temp = new Node(d);
temp->next = head;
head-> prev = temp;
head=temp;
}
int insertAtTail(Node*tail, int d){
Node* temp = new Node(d);
temp->prev = tail;
tail->next = temp;
tail=temp;
}
void insertAtpos(Node* &head,Node* &tail, int pos, int d){
if(pos==1){
insertAtHead(head, d);
return;
}
Node* temp = head;
int count =1;
while(count<pos-1){
count ++;
temp=temp->next;
}
if(temp->next==NULL){
insertAtTail(tail, d);
return;
}
Node* newNodeinsert = new Node(d);
newNodeinsert->next=temp->next;
temp->next->prev=newNodeinsert;
temp-> next=newNodeinsert;
newNodeinsert->prev=temp;
}
void deleteion(Node* &head, int pos){
if(pos==1){
Node* temp=head;
head=temp->next;
temp->next->prev=NULL;
temp->next=NULL;
return;
}
else{
Node* prev = NULL;
Node* curr = head;
int count =1;
while(count<pos){
count ++;
prev=curr;
curr=curr->next;
}
curr->prev=NULL;
prev->next=curr->next;
curr->next=NULL;
}
}
int main(){
Node* node1= new Node(10);
Node* head = node1;
Node* tail = node1;
print(head);
insertAtHead(head, 32);
print(head);
insertAtHead(head, 13);
print(head);
insertAtTail(tail, 19);
print(head);
insertAtpos(head, tail, 3, 76);
print(head);
insertAtpos(head, tail, 6, 76);
print(head);
insertAtpos(head, tail, 1, 76);
print(head);
deleteion(head, 1);
print(head);
deleteion(head, 3);
print(head);
deleteion(head, 4);
print(head);
return 0;
}