-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
insertInASortedLinkedList.cpp
64 lines (59 loc) · 1.58 KB
/
insertInASortedLinkedList.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
/**
* Given a sorted linkedlist, insert a new node in it, so that list remains sorted.
*/
#include <iostream>
struct Node {
int data;
Node * next;
Node ( int d ) : data{ d }, next{ nullptr } { }
};
void sortedInsert( Node * & head, int data )
{
Node * newNode = new Node( data );
if ( head == nullptr ||
((head != nullptr) && (head->data >= newNode->data))) {
newNode->next = head;
head = newNode;
} else {
Node * currNode = head;
while ( currNode->next != nullptr && (currNode->next->data < newNode->data)) {
currNode = currNode->next;
}
newNode->next = currNode->next;
currNode->next = newNode;
}
}
void printList( Node * head )
{
while ( head ) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "NULL" << std::endl;
}
int main()
{
Node * head = nullptr;
std::cout << "Inserting 3 to the List, List becomes\n";
sortedInsert( head, 3 );
printList( head );
std::cout << "Inserting 1 to the List, List becomes\n";
sortedInsert( head, 1 );
printList( head );
std::cout << "Inserting 2 to the List, List becomes\n";
sortedInsert( head, 2 );
printList( head );
std::cout << "Inserting 5 to the List, List becomes\n";
sortedInsert( head, 5 );
printList( head );
std::cout << "Inserting 4 to the List, List becomes\n";
sortedInsert( head, 4 );
printList( head );
std::cout << "Inserting 2 to the List, List becomes\n";
sortedInsert( head, 2 );
printList( head );
std::cout << "Inserting 6 to the List, List becomes\n";
sortedInsert( head, 6 );
printList( head );
return 0;
}