C语言编程:向ASC排序的单链表中插入一个节点
来源:百度知道 编辑:UC知道 时间:2024/06/25 23:17:38
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int nVal;
node* pNext;
};
node* InserNode(node *p, node *pNode);
void main()
{
node *pNode, *f=NULL;
srand((unsigned)time(NULL));
// 生成
for (int i=0; i<10; i++)
{
pNode = new node;
pNode->nVal = rand();
pNode->pNext = NULL;
f = InserNode(f, pNode);
}
// 输出
node *tmp = f;
while (tmp != NULL)
{
cout << tmp->nVal << ",";
tmp = tmp->pNext;
}
cout << endl;
}
node* InserNode(node *p, node *pNode)
{
node *pre=p, *t=p;
if (p == NULL)
{
p = pNode;
return p;
}
while (t != NULL && pNode->nVal > t->nVal)
{
pre = t;
t = t->pNext;
}
if (t == N