# Selection of a single contact list

• No way to sort the list, tell me where the mistake is.

I've set up two single lists. One empty list to which a separate list is to be placed and the other to a single reference list. With each loss, we remove the node from our primary list and put it in the right position of the disserted list. The grading is performed by two functions. The first function (SortedInsert) is looking for a space on a separate list where to insert the knot. The second function(InsertionSort) is directly the sorting in which the node is selected from the reference list, which should be added to the new list and placed under the first function. So in the second function, we're taking into account when we have to put the knot first on the list.

Here's the algorithm of the boxing.

{

result-&gt;next = NULL;

while(ptr)
{
PNode temp = ptr;
ptr = ptr-&gt;next;
result = SortedInsert(result, temp);
}

return result;

}

Through this function, there is a place where to insert a knot in a new set:

{
{
}

PNode prev = NULL;

while(ptr != NULL &amp;&amp; ptr-&gt;data &lt; NewNode-&gt;data)
{
prev = ptr;
ptr = ptr-&gt;next;
}

NewNode-&gt;next = ptr;
prev-&gt;next = NewNode;

}

All code with the addition and display of the list:

#include <iostream>

using namespace std;

struct Node
{
int data;
Node *next;
};

typedef Node *PNode;

int main()
{
int node;

cout &lt;&lt; "Enter 5 members of the linked list!" &lt;&lt; endl;

for(int i = 0; i &lt; 5; i++)
{
cin &gt;&gt; node;
}

cout &lt;&lt; endl;

return 0;
}

{
PNode NewNode = new Node;
NewNode-&gt;data = data;
cout &lt;&lt; "Done!" &lt;&lt; endl;
}

{
while(q)
{
cout &lt;&lt; q-&gt;data &lt;&lt; " ";
q = q-&gt;next;
}
}

• You have to write at least main.

Otherwise, you will not remove the sorted list because you do not retain the result of the method at any parent node declared in the main.

There is also a side problem that the reference knot does not change. I mean, two lists point to the same original node.

For the rest, in principle, the functions are correctly written.

I would use the following approach to moving one list to another and sorting its elements.

In the method of implementation, the indicator on the head knot is transmitted by reference. The other way is to transfer the index to the main knot into methods that can change the main knot.

You can use this program as a model for your program.

#include <iostream>
#include <cstdlib>
#include <ctime>

typedef struct Node
{
int data;
Node *next;
} *PNode;

void push_front( PNode &head, int data )
{
}

std::ostream & display( const PNode &head, std::ostream &os = std::cout )
{
for ( PNode current = head; current; current = current->next )
{
os << current->data << ' ';
}
return os;
}

void SortedInsert( PNode &head, PNode node )
{
{
}
else
{
while ( current->next && !( node->data < current->next->data ) )
{
current = current->next;
}

node-&gt;next = current-&gt;next;
current-&gt;next = node;
}

}

{
PNode result = nullptr;

{
SortedInsert( result, node );
}

return result;

}

int main()
{
const size_t N = 10;

std::srand( ( unsigned int )std::time( nullptr ) );

for ( size_t i = 0; i &lt; N; i++ ) push_front( head, std::rand() % N );

return 0;

}

The withdrawal to the console may be as follows:

7 2 2 1 0 5 4 1 4 1
0 1 1 1 2 2 4 4 5 7

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2