Issue when adding element into sorted linked list C++
By : user3049905
Date : March 29 2020, 07:55 AM
it fixes the issue That's because cur is a reference in add_ordered_i and you have cur = cur>next which also modify l. I made a few changes and it works for me. code :
template<class T>
void add_ordered_i (LN<T>*& l, T value) {
LN<T>* cur = l;//just a normal pointer will be fine
if(cur == nullptr) {
l = new LN<T>(value);//a empty list
return;
}
while(cur>next != nullptr)
{
if(value < cur>next>value)
break;
cur = cur>next;
}
if(cur == l) {
l = new LN<T>(value, cur);//add to head of list
} else {
cur>next = new LN<T>(value,cur>next);
}
}

Adding element to a sorted list
By : JAMES
Date : March 29 2020, 07:55 AM
Hope that helps The error only occurs when your second call for ListEl has a value for key larger than your first call. That's why the code was failing ~%50 of the time, because it all depends on which numbers were randomly called. As for "sometimes doesn't add 20 numbers and stop at 19", I'm not sure why this was happening, as I cannot reproduce this. To fix your problem, from this answer, "If you need a pointer of a typedeffed type, declare an instance of one, but keep your typedefs to types so an not to mask levels of indirection." code :
int i;
srand(time(NULL));
ListEl a = {0, NULL};
ListEl* head = &a;
head = PushFront(head, rand()%100);
for (i = 0; i < 20; i++)
head = PushFrontsort(head, rand()%100);
typedef struct _ListEl {
int key;
struct _ListEl *next;
} ListEl;

Haskell, own datatype, adding an Element to an sorted list
By : Данил Сидоренко
Date : March 29 2020, 07:55 AM
it should still fix some issue working version with minimalistic changes, thx @Matt, Petr and Landei!!! , I see two major problems with ins:

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list"
By : zsigmonda
Date : March 29 2020, 07:55 AM

Insert a node into a sorted linked list of integers such that the list still remains sorted with final members for next
By : Rodrive
Date : September 07 2020, 12:00 AM
it fixes the issue When the next link is final: The only theoretical way would be to add a new node in front of the list and shift the data: code :
void insertSorted(MyList list, int data) {
list.head = new Node(0, list.head); // Insert in front;
Node prior = list.head;
// Invariant condition: prior points to a node (not null) and soon data >= prior.data
Node current = prior.next;
while (current != null) {
if (data < current.data) {
break;
}
prior.data = current.data; // Shift smaller
prior = current;
current = current.next;
}
prior.data = data;
}
insert: d
list.head: a ; b ; c ; e ; f ; g

list.head: X ; a ; b ; c ; e ; f ; g
a <´  
b <´ 
c <´
d

