-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathinsert-a-node-into-a-sorted-doubly-linked-list.cpp
More file actions
166 lines (136 loc) · 3.36 KB
/
Copy pathinsert-a-node-into-a-sorted-doubly-linked-list.cpp
File metadata and controls
166 lines (136 loc) · 3.36 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Inserting a Node Into a Sorted Doubly Linked List
// Create a node with a given value and insert it into a sorted doubly-linked list
//
// https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem
//
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
struct Node
{
int data;
Node* next;
Node* prev;
};
//---------------------------------------------------------------------------------------------------
/*
Insert Node in a doubly sorted linked list
After each insertion, the list should be sorted
Node is defined as
struct Node
{
int data;
Node *next;
Node *prev;
}
*/
Node* SortedInsert(Node *head,int data)
{
/// { static bool first = true; if (first) { first = false; system("cat solution.cc >&2") == 0; } }
// Complete this function
// Do not write the main method.
Node *nouv;
nouv = new Node;
nouv->next = nullptr;
nouv->prev = nullptr;
nouv->data = data;
if (head == NULL)
{
return nouv;
}
if (data < head->data)
{
nouv->next = head;
head->prev = nouv;
return nouv;
}
Node *node = head;
for (node = head; node->next != nullptr && node->next->data < data; node = node->next);
if (node->next != nullptr)
{
nouv->next = node->next;
node->next->prev = nouv;
}
node->next = nouv;
nouv->prev = node;
return head;
}
//---------------------------------------------------------------------------------------------------
Node * SortedInsertHidden(Node * head, int data)
{
Node *current = NULL;
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data=data;
new_node->next=NULL;
new_node->prev=NULL;
if (head == NULL )
{
head = new_node;
}
else if(head->data >= new_node->data)
{
new_node->next = head;
head->prev=new_node;
head = new_node;
}
else
{
current = head;
while (current->next!=NULL && current->next->data < new_node->data)
{
current = current->next;
}
if(current->next!=NULL)
{
new_node->next = current->next;
current->next->prev=new_node;
}
current->next = new_node;
new_node->prev=current;
}
return head;
}
void Print(Node *head) {
if(head == NULL) return;
while(head->next != NULL){ cout<<head->data<<" "; head = head->next;}
cout<<head->data<<" ";
while(head->prev != NULL) { cout<<head->data<<" "; head = head->prev; }
cout<<head->data<<"\n";
}
int checker(Node * head1 , Node * head2)
{
if(head1==NULL && head2==NULL)return 1;
if(head1==NULL)return 0;
if(head2==NULL) return 0;
if(head1->data!=head2->data)
return false;
return checker(head1->next,head2->next);
}
int main()
{
int t; cin>>t;
Node *head = NULL;
Node * head2=NULL;
string S="Some possible errors:\n1. You returned a NULL value from the function. \n2. There is a problem with your logic";
while(t--) {
int n; cin>>n;
head = NULL;
head2 = NULL;
for(int i = 0;i<n;i++) {
int x; cin>>x;
head = SortedInsert(head,x);
head2=SortedInsertHidden(head2,x);
}
if(checker(head,head2)==1)
{
cout<<"Right Answer!\n";
}
else
{
cout<<"Wrong Answer!\n";
cout<<S<<endl;
}
}
}