Enter an ascending one-way list and a linked list node to insert the node in ascending order in the unidirectional list.
The case in which the input is a null pointer is treated as an exception, regardless of the value of the node being equal.
The linked list node is defined as follows:
struct ListNode
{
int M_nkey;
listnode* M_pnext;
};
Detailed Description:
Interface description
Prototype:
listnode* insertnodetolist (listnode* plisthead, listnode* pinsertnode);
Input parameters:
listnode* Plisthead unidirectional linked list
listnode* Pinsertnode New Insert Node
Output parameters (the memory area pointed to by the pointer is guaranteed to be valid):
listnode* Plisthead unidirectional linked list
return value:
Normal Insert node returns a chain header pointer, other exception returns a null pointer
#include "OJ.h"
/* Function: Enter an ascending one-way list and a list node to insert this node in ascending order in the unidirectional list. The case in which the
input is a null pointer is treated as an exception, regardless of the value of the node being equal.
input: listnode* plisthead unidirectional linked list
listnode* pinsertnode new Insert node
output: listnode* plisthead One-way linked list
returns: Normal Insert node returns the link header pointer, other exception returns null pointer
*
/listnode* insertnodetolist (listnode* plisthead, listnode* pinsertnode)
{ /
* Implement function here */
if (Plisthead = = NULL | | pinsertnode = = NULL)
return (listnode*) null;
ListNode *p = Plisthead,*pnext = Plisthead;
while (pnext!= NULL && Pnext->m_nkey < Pinsertnode->m_nkey)
{
p = pnext;
Pnext = pnext->m_pnext;
}
Insert Head pointer position
if (Pnext = = Plisthead)
{
pinsertnode->m_pnext = Plisthead;
Plisthead = Pinsertnode;
}
else
{
pinsertnode->m_pnext = Pnext;
P->m_pnext = Pinsertnode;
}
return plisthead;
}