#include
#include
typedef struct lnode
{ int data;
struct lnode *next;
}LNode,*LinkList;
LNode* Creat_LNode ( ) // 创建链表
{
LNode *s,*r,*L;
int x;
scanf ( "%d",&x );
L = r = ( LNode * ) malloc ( sizeof( LNode ));
L->next = NULL;
while ( x != 00 )
{
s = ( LNode * ) malloc ( sizeof( LNode ));
s->data = x;
if ( L->next == NULL )
L->next = s;
else
r->next = s;
r = s;
scanf ( "%d",&x );
}
if ( r != NULL )
r->next = NULL;
return L;
}
LNode *Locate_LNode1(LNode *L,int x) //寻址 ,判断原链表中是否存在与x值相等的节点
{
LNode *p=L->next;
while ( p !=NULL && p->data != x)
p = p->next;
return p;
}
LNode *Locate_LNode2(LNode *L,int x)
{
LNode *p=L;
while ( p != NULL )
{
p = p->next;
if ( p->data > x )
{
return p;
}
else
if ( p->next = NULL && p->data < x )
{
return p;
}
}
return NULL;
}
void Insert_LNode( LNode *L,LNode *p,int x )
{
LNode *q,*s;
s=(LNode *)malloc(sizeof(LNode));
s->data = x;
q=L;
if ( p->data > x )
{
while ( q->next != p)
q=q->next;
s->next = p;
q->next = s;
}
else
{
s->next = NULL;
p->next = s;
}
}
void main ()
{
LNode * L=NULL;
LNode * p=NULL;
int x;
LNode * q=NULL;
LNode * w=NULL;
printf("请输入带头结点单链表L中的整数值(输入以00为结束的标志):");
L=Creat_LNode();
printf("请输入要插入的整数值:");
scanf("%d",&x);
p=Locate_LNode1(L,x);
if ( p != NULL )
printf ("该整数值已存在,不需要再插入!");
else
{
q=Locate_LNode2(L,x);
Insert_LNode(L,q,x);
printf("插入成功!");
printf("插入后的链表为:\n");
w = L->next;
while ( w != NULL )
{
printf("%d\n",w->data);
w=w->next;
}
}
printf("\n");
}