Inserts a node into an ascending one-way list

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.