-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathInsertioninSortedLinkedList.cpp
70 lines (63 loc) · 1.35 KB
/
InsertioninSortedLinkedList.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
70
/* Program to Insert in a sorted list */
#include <iostream>
using namespace std;
/* Node */
class Node {
public:
int data;
Node* next;
};
/* Function to insert in a sorted linked list*/
void sortedInsert(Node** head_ref, Node* new_node)
{
Node* current;
if (*head_ref == NULL
|| (*head_ref)->data
>= new_node->data) {
new_node->next = *head_ref;
*head_ref = new_node;
}
else {
current = *head_ref;
while (current->next != NULL && current->next->data < new_node->data) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* Function to create a new node */
Node* newNode(int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
/* Function to print linked list */
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
/* Driver Program*/
int main()
{
Node* head = NULL;
char c = 'y';
int n;
do{
cout<<"\nEnter value of new node : ";
cin>>n;
Node* new_node = newNode(n);
sortedInsert(&head, new_node);
cout<<"\nDo you want to add more nodes ? (y/n)";
cin>>c;
}while(c == 'y');
cout<<"\nCreated Linked List\n";
printList(head);
return 0;
}