-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdoublyLL.cpp
63 lines (56 loc) · 1.05 KB
/
doublyLL.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node *prev;
Node(int data)
{
this->data = data;
next = NULL;
prev = NULL;
}
};
Node *insertNode(Node *head, int n, int pos)
{
Node *newNode = new Node(n);
Node *temp = head;
if (pos == 0)
{
newNode->next = head;
head = newNode;
}
else if (temp != NULL)
{
for (int i = 0; i < pos - 1; i++)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
newNode->prev = temp;
newNode->next->prev = newNode;
}
return head;
}
void print(Node *head)
{
Node *temp = head;
cout << temp->data << " ";
temp = temp->next;
print(temp);
}
int main()
{
Node *head = new Node(10);
Node *head1 = new Node(20);
Node *head2 = new Node(5);
head->next = head1;
head->prev = head2;
head1->prev = head;
head2->next = head;
head2 = insertNode(head2, 100, 0);
print(head2);
}