在单链表中值为x的前面加入y值;并判断单链表排序是否有序(我是将单链表值直接赋值给一个数组在来判断)
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LinkList //创建一个节点
{
ElemType data;
struct LinkList next;
}L_List;
L_List * createlist() //创建单链表
{
L_List *head,
r,p;
ElemType x;
head=r=NULL;
scanf("%d",&x);
while(x)//以输入0为结束
{
p=(L_List *)malloc(sizeof(L_List));//尾插法
p->data=x;
if(head==NULL)
head=p;
else
r->next=p;
r=p;
scanf("%d",&x);
}
r->next=NULL;
return head;
}
L_List * insert(L_List *L,ElemType x,ElemType y) //在值为Y的节点前加入值为x的节点
{
L_List *p=L,
r,*s;
if(L->data==y)
{
s=(L_List *)malloc(sizeof(L_List));
s->data=x;
s->next=L;
L=s;
}
else
{
while(p&&p->data!=y)
{
r=p;
p=p->next;
}
if(!p)
printf("error!!!");
else
{
s=(L_List *)malloc(sizeof(L_List));
s->data=x;
s->next=r->next;
r->next=s;

}
    }
p=L;
for(;p!=NULL;)
{
    printf("%-3d",p->data);
    p=p->next;
}
printf("\n");
return L;

}
L_List *judge(L_List *L) //判断链表是否有序
{
L_List *p=L;
ElemType a[100],sum=0,x=0,y=0,i;
while(p)
{
a[sum]=p->data;
p=p->next;
sum++;
}
for(i=0;i<sum+1;i++)
{
if(a[i]<=a[i+1])
x++;
if(a[i]>=a[i+1])
y++;
}
if(x==sum)
printf("此链表为递增序列\n");
else if(y==sum)
printf("此列表为递减序列\n");
else printf("此列表是乱序\n");

}
int main()
{
L_List *head;
printf("请输入一组数据以0结束:\n");
head=createlist();
judge(head);
int x,y;
printf("请输入x,y的值:\n");

scanf("%d%d",&x,&y);
head=insert(head,x,y);

return 0;

}