-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinsert_node_in_double_linked_list.cpp
More file actions
37 lines (35 loc) · 1008 Bytes
/
Copy pathinsert_node_in_double_linked_list.cpp
File metadata and controls
37 lines (35 loc) · 1008 Bytes
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
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* llist, int data) {
DoublyLinkedListNode *node = new DoublyLinkedListNode(data);
node->next = node->prev = NULL;
DoublyLinkedListNode *head = llist;
// Check il ll is empty;
if(head == NULL)
{
return node;
}
// Check first node
if(head->data > data)
{
node->next = head;
return node;
}
while(head->next != NULL && head->next->data <= data )
{
head = head->next;
}
// Check if we have reached the end;
if(head->next == NULL )
{
// We are at the last node, so we have to add now the new element at tail
head->next = node;
node->prev = head;
node->next = NULL;
return llist;
}
// We have to insert the new node between two existing node
node->next = head->next;
cout<<"cambio testa "<< head->data << ' ' <<node->data <<endl;
head->next = node;
node->prev = head;
return llist;
}