#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 return: Normal Insert node returns the chain header pointer, other exceptions return null Pointer */listnode* insertnodetolist (listnode* plisthead, listnode* pinsertnode) {/* Implement function here */if ((ListNode *) NULL = = Plisthea d) {return (ListNode *) (NULL);} if ((ListNode *) NULL = = Pinsertnode) {return (ListNode *) (NULL); } if (Pinsertnode->m_nkey < Plisthead->m_nkey) {pinsertnode->m_pnext = Plisthead; Plisthead = Pinsertnode; Return (ListNode *) Plisthead; }listnode* p = plisthead; while ((ListNode *) NULL! = P->m_pnext && P->m_pnext->m_nkey < pinsertnode->m_nkey) {p = p->m_pnext; } Pinsertnode->m_pnext = p->m_pnext; P->m_pnext = Pinsertnode;return (ListNode *) Plisthead;} int main () {ListNode astlistnode[2]; ListNode Stinsertnode; ListNode *plisthead Astlistnode[0].m_nkey = 2; Astlistnode[0].m_pnext = &astListNode[1]; Astlistnode[1].m_nkey = 3; Astlistnode[1].m_pnext = (ListNode *) (NULL); Stinsertnode.m_nkey = 4; Stinsertnode.m_pnext = (ListNode *) (NULL); Plisthead = Insertnodetolist (Astlistnode, &stinsertnode); return 0;}
Inserts a node into an ascending one-way list