如何定义和调用函数和链表的插入

给定程序中已建立一个带有头结点的单向链表,链表中的各结点按结点数据域中的数据递增有序链接。把形参x的值放入一个新结点并插入到链表中,插入后个结点仍保持递增有序。

img

img

img

img


img

供参考:

void nodeInstert(NODE *h, int x)
{
    /**********Begin***********/
    NODE *pt = h, *ptn = NULL;
    ptn = (NODE *)malloc(sizeof(NODE));
    ptn->data = x;
    ptn->next = NULL;
    while (pt->next && pt->next->data < x) pt = pt->next;
    ptn->next = pt->next;
    pt->next = ptn;
    /***********End***********/
}

void nodeInstert(NODE *h, int x)
{
    NODE * newp = (NODE *)malloc(sizeof(NODE));
    newp->data = x;
    while (h->next != NULL)
    {
        if (h->data > x)
        {
            newp->next = h->next;
            h->next = newp;
            return;
        }
        h = h->next;
    }
    h->next = newp;
    newp->next = NULL;
}